简体   繁体   English

如何使用初始化的数据库创建Docker映像

[英]How can I create a docker image with a initialized database

I want to use the official mysql docker image to create an new image with an initialized database and imported dump file. 我想使用官方的mysql docker映像来创建一个具有初始化数据库和导入转储文件的新映像。 The default command is mysqld , so I wonder how I can achieve this. 默认命令是mysqld ,所以我想知道如何实现它。 Is there a way to only initialize the database and import a dump, without starting the mysql daemon? 有没有一种方法可以只初始化数据库并导入转储,而无需启动mysql守护程序?

The MySQL docker images automatically parse .sh .sql and .sql.gz from the /docker-entrypoint-initdb.d directory on first start up. 第一次启动时,MySQL .sql.gz镜像会自动从/docker-entrypoint-initdb.d目录中解析.sh .sql.sql.gz Files will be executed in alphabetical order. 文件将按字母顺序执行。

Dump your existing database to a file, then edit your Dockerfile to add: 将现有数据库转储到文件中,然后编辑Dockerfile以添加:

ADD mysqldump.sql /docker-entrypoint-initdb.d/mysqldump.sql

On first start, mysqldump.sql will be imported. 首次启动时,将导入mysqldump.sql

An option would be to access the bash shell of the docker container (please see "Container shell access and viewing MySQL logs" section in https://hub.docker.com/_/mysql/ ), import your dump file (you can use docker cp to copy the dump file from your host system to the docker container) and once you are done, you can use docker commit command to create a new docker image from your current running container. 一种选择是访问docker容器的bash shell(请参见https://hub.docker.com/_/mysql/中的 “容器shell访问和查看MySQL日志”部分),导入转储文件(您可以使用docker cp将转储文件从主机系统复制到docker容器),完成后,您可以使用docker commit命令从当前正在运行的容器中创建新的docker映像。 And you can use the newly created image afterwards. 然后您可以使用新创建的图像。

Hope this helps. 希望这可以帮助。

I just found a solution for mysql:5.7 . 我刚刚找到了mysql:5.7的解决方案。 I execute the slightly modified entrypoint.sh during the build process: 我在构建过程中执行了稍微修改的entrypoint.sh

COPY ./docker-entrypoint.sh /
RUN /docker-entrypoint.sh mysqld

In the docker-entrypoint.sh I deleted the last line: docker-entrypoint.sh我删除了最后一行:

exec "$@

It looks ugly, but it works so far. 它看起来很丑,但是到目前为止它仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM