英文:
can't load sql dump to the containered mariadb
问题
我正在尝试创建一个基于Docker的MariaDB数据库。在构建过程中,我需要加载SQL转储文件。
以下是我的Dockerfile。
FROM alpine:3.18.2
WORKDIR /Database
COPY . /Database
# 安装MySQL及其依赖项
RUN apk update && apk upgrade
RUN apk add --no-cache mysql mysql-client openrc bash
# 启动MySQL服务器
RUN mkdir /run/mysqld
RUN mysql_install_db --user=root
RUN mysqld -u root --data=./data & > /dev/null &
#RUN rc-service mariadb start
# 将数据加载到数据库
RUN mariadb -u root -e "CREATE DATABASE passwords;"
RUN mariadb -u root -p < dump.sql
EXPOSE 3306
ENTRYPOINT ["./entrypoint.sh"]
但是,在构建容器时,我收到以下错误:
ERROR 2002 (HY000): 无法通过套接字'/run/mysqld/mysqld.sock'(2)连接到本地服务器
据我了解,这似乎是在MariaDB未运行时出现的错误。但如果我取消注释 RUN rc-service mariadb start
,则会收到另一个错误:
* 警告: mariadb 正在启动
命令'/bin/sh -c rc-service mariadb start'返回了一个非零代码: 1
我尝试进入容器的shell并手动启动MariaDB,但未成功。
英文:
I am trying to make a dockerized mariadb database. In this database i need to load sql dump while build.
Here is my Dockerfile.
FROM alpine:3.18.2
WORKDIR /Database
COPY . /Database
#install mysql and dependencies
RUN apk update && apk upgrade
RUN apk add --no-cache mysql mysql-client openrc bash
# start mysql server
RUN mkdir /run/mysqld
RUN mysql_install_db --user=root
RUN mysqld -u root --data=./data &> /dev/null &
#RUN rc-service mariadb start
# load data to database
RUN mariadb -u root -e "CREATE DATABASE passwords;"
RUN mariadb -u root -p < dump.sql
EXPOSE 3306
ENTRYPOINT ["./entrypoint.sh"]
But when I build container, I get this error:
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)
As i understand it appears when mariadb is not running, but if i uncomment the RUN rc-service mariadb start
i will get another error:
* WARNING: mariadb is already starting
The command '/bin/sh -c rc-service mariadb start' returned a non-zero code: 1
i have tried to enter the container's shell and start mariadb manualy but it was unsuccessful.
答案1
得分: 0
Docker构建阶段是独立的,你不能在RUN
阶段运行后台进程并让它们在下一步中继续运行。
rc-service
、systemctl
、service
等命令无法运行,因为没有正在运行的init
或openrc进程,这些命令会连接到这些进程。
生成容器的最简单方法是以初始镜像为基础:
FROM mariadb:lts
COPY dump.sql /docker-entrypoint-initdb.d
这将在启动时初始化数据库。
如果你想要一个真正预生成的镜像:
FROM mariadb:lts
COPY dump.sql /
RUN echo '[mariadb]\ndatadir=/data' > /etc/mysql/conf.d/localdata.cnf ; mkdir /data ; mariadb-install-db ; mariadbd --bootstrap < dump.sql; chown -R mysql: /data ; rm /dump.sql
请注意,这不会设置任何数据库密码。此外,dump.sql
将以以下方式开始:
CREATE DATABASE passwords;
use passwords;
...
create table ....
英文:
Docker build stages are independent and you can't run background process in the RUN
stages and have them still running on the next step.
rc-service
, systemctl
, service
etc won't run because there is no init
or openrc process running which these commands connect to.
The easiest way to generate a container is to base it on the initial image:
FROM mariadb:lts
COPY dump.sql /docker-entrypoint-initdb.d
This will initialize the database on startup.
If you want a truly pre-generated image:
FROM mariadb:lts
COPY dump.sql /
RUN echo '[mariadb]\ndatadir=/data' > /etc/mysql/conf.d/localdata.cnf ; mkdir /data ; mariadb-install-db ; mariadbd --bootstrap < dump.sql; chown -R mysql: /data ; rm /dump.sql
Note this won't have any database passwords setup. Also the dump.sql
will begin with:
CREATE DATABASE passwords;
use passwords;
...
create table ....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论