简体   繁体   English

在构建时在Docker容器中创建MySQL数据库

[英]Create MySQL database in Docker container at build time

I need to create a Docker image with a large database. 我需要使用大型数据库创建Docker映像。 The database can be populated using a script populate_net_db.sh from specified folders. 可以使用脚本populate_net_db.sh从指定的文件夹中populate_net_db.sh数据库。 I need to create a Docker image that has this done at build time so that developers working with database can create a container and not wait long time before the database gets populated. 我需要创建在构建时完成此操作的Docker映像,以便使用数据库的开发人员可以创建容器,而不必等待很长时间才能填充数据库。

What I have done (this worked but is not what I need): 我做了什么(这有效,但不是我需要的):

  • I can create database at run time and populate it using mysql Docker image and putting the required files into docker-entrypoint-initdb.d folder. 我可以在运行时创建数据库,并使用mysql Docker映像填充它,并将所需文件放入docker-entrypoint-initdb.d文件夹中。 This will require significant time to set things up when you run the image, but works as needed and I can access the DB with docker exec -it "image_name" mysql -u root -p exactly as required. 运行映像时,这将需要花费大量时间来进行设置,但是可以根据需要进行工作,而我可以根据需要使用docker exec -it "image_name" mysql -u root -p访问数据库。

  • I can create the same database at build time using RUN command and see that the tables are set up correctly at build, however, when I run such an image a new mysql DB is set up and the DB from build is gone. 我可以在构建时使用RUN命令创建相同的数据库,并查看在构建时正确设置了表,但是,当我运行此类映像时,将建立新的mysql DB,而构建时的DB消失了。

Is there a way to have the build-time database show up in docker exec -it "image_name" mysql -u root -p ? 有没有办法在docker exec -it "image_name" mysql -u root -p显示构建时数据库?

I managed to solve this problem by saving the contents of /var/lib/mysql/ in a temporary folder during build as: mv /var/lib/mysql /mnt/tmp/ . 我设法通过保存的内容来解决这个问题/var/lib/mysql/编译期间作为在临时文件夹: mv /var/lib/mysql /mnt/tmp/ Then I created an entrypoint script to put in /docker-entrypoint-initdb.d folder. 然后,我创建了一个入口点脚本来放入/docker-entrypoint-initdb.d文件夹。 This script deletes the /var/lib/mysql/ contents at run time and moves the saved state from /mnt/tmp/ . 该脚本在运行时删除/var/lib/mysql/内容,并将保存的状态从/mnt/tmp/移出。 This causes MySQL to restart. 这将导致MySQL重新启动。 However, the database becomes available very fast at container run. 但是,数据库在容器运行时变得非常快速可用。 This method takes ~5 minutes for a 27GB database versus >1hr with loading the database from a dump file. 对于27GB的数据库,此方法大约需要5分钟,而从转储文件中加载数据库需要1小时以上。

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

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