在docker build中播种mysql数据 [英] seeding mysql data in a docker build
问题描述
我正在尝试构建一个包含MySQL和一些种子数据的docker映像,并且试图弄清楚在docker构建阶段如何将数据插入数据库。
I'm attempting to build a docker image that will include MySQL and some seed data, and I'm trying to figure out how to insert the data into the database during the docker build phase.
似乎我需要启动MySQL引擎,调用命令来运行一些SQL语句,然后关闭MySQL引擎。关于如何最好地做到这一点的任何好主意?
It seems I need to start the MySQL engine, invoke a command to run some SQL statements, and then shut down the MySQL engine. Any good ideas on how to best do that?
推荐答案
构建映像时,文件夹 /检查docker-entrypoint-initdb.d
是否有文件作为数据库的种子。在官方MySQL Docker上的初始化新实例部分的相应段落下方中心页面。
When the image is built, the folder /docker-entrypoint-initdb.d
is checked for files to seed the DB with. Below the corresponding paragraph from the section Initializing a fresh instance on the official MySQL Docker Hub page.
首次启动容器时,将创建一个具有指定名称的新数据库,并使用提供的数据库进行初始化。配置变量。此外,它将执行扩展名为
.sh
,.sql
和.sql.gz的文件在
。文件将按字母顺序执行。您可以通过将SQL转储安装到该目录中并为自定义图像提供贡献的数据来轻松填充mysql服务。默认情况下,SQL文件将导入到MYSQL_DATABASE变量指定的数据库中。/docker-entrypoint-initdb.d
中找到的
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions
.sh
,.sql
and.sql.gz
that are found in/docker-entrypoint-initdb.d
. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
这篇关于在docker build中播种mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!