[英]MySQL Docker container is not saving data to new image
I'm trying to create a MySQL Docker container that is preset with a certain schema and seed data so that I can have other containers connect to it as a db. 我正在尝试创建一个预设有某种模式和种子数据的MySQL Docker容器,这样我就可以将其他容器作为数据库连接到它。 I'm using the trusted dockerfile/mysql image as a base, and I wrote a Dockerfile to create a new image from that base and add my schema.sql into it. 我使用可信的dockerfile / mysql映像作为基础,我编写了一个Dockerfile来从该库创建一个新映像并将schema.sql添加到其中。 After building that image (mysql:base), I've been trying to run bash in new container, then go into mysql and create my database and then import the schema. 在构建该映像(mysql:base)之后,我一直在尝试在新容器中运行bash,然后进入mysql并创建我的数据库然后导入模式。 I then exit the container and try to commit the container to a new Docker image. 然后我退出容器并尝试将容器提交到新的Docker镜像。 However, the resulting image does not persist any of the changes I made to the MySQL db. 但是,生成的图像不会保留我对MySQL数据库所做的任何更改。 It does persist other files that I wrote in the container, but not the db. 它确实持有我在容器中写入的其他文件,但不包括数据库。
Here is the Dockerfile I use to build the initial image (myorg/mysql:base). 这是我用来构建初始图像的Dockerfile(myorg / mysql:base)。
FROM dockerfile/mysql:latest
MAINTAINER (me)
ADD schema.sql /data/schema.sql
EXPOSE 3306
# Define working directory.
WORKDIR /data
CMD ["mysqld_safe"]
After building that, I go into the image: 建立之后,我进入图像:
docker run -i -t myorg/mysql:base bash
And run MySQL to import the schema: 并运行MySQL以导入架构:
myslqd_safe &
141218 00:15:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql -u root
mysql> CREATE DATABASE mydb;
exit;
mysql -u root -D mydb < schema.sql
I can go into mysql and verify the schema has been imported successfully: 我可以进入mysql并验证架构是否已成功导入:
mysql -u root -D mydb -e "SELECT * from tokens;"
Also, if I go into /var/lib/mysql
I can see that there is a mydb
directory that contains .frm files corresponding to the db. 另外,如果我进入/var/lib/mysql
我可以看到有一个mydb
目录,其中包含与db对应的.frm文件。
But when I exit and try to commit that container to a new image: 但是当我退出并尝试将该容器提交到新图像时:
docker commit -m="import schema.sql" -a="Me" 72c2ff39dd65 myorg/mysql:seed
And then go into the new image: 然后进入新的形象:
docker run -i -t --rm myorg/mysql:seed bash
The db files are no longer in /var/lib/mysql, and running mysql -u root -e "SHOW DATABASES"
does not show the mydb
database, only the default mysql
, information_schema
, and performance_schema
dbs. db文件不再位于/ var / lib / mysql中,并且运行mysql -u root -e "SHOW DATABASES"
不会显示mydb
数据库,只显示默认的mysql
, information_schema
和performance_schema
dbs。 I found that if I created a new textfile in the container ( echo 'test' > newfile
), that file would be present in the committed image, but not the db. 我发现如果我在容器中创建了一个新的文本文件( echo 'test' > newfile
),那么该文件将出现在已提交的映像中,而不是数据库中。
I wonder if this has something to do with the fact that the trusted image Dockerfile has VOLUME ["/etc/mysql", "/var/lib/mysql"]
so it mounts the db directory as a volume. 我想知道这是否与可信图像Dockerfile具有VOLUME ["/etc/mysql", "/var/lib/mysql"]
这一事实有关,因此它将db目录作为卷安装。 My Dockerfile does not have this command, but I don't know if it's inherited anyway (I don't really understand how volumes work well enough to know how this might affect my build). 我的Dockerfile没有这个命令,但我不知道它是否继承了(我真的不明白卷如何运行得足以知道这可能会如何影响我的构建)。 I don't need the db mounted as a volume because I need another container to connect to it over a network connection (I'm going to use docker links for that). 我不需要将db作为卷安装,因为我需要另一个容器通过网络连接连接到它(我将使用docker链接)。
FWIW, I am running boot2docker 1.3.2 on OS X 10.9.5. FWIW,我在OS X 10.9.5上运行boot2docker 1.3.2。
As another answer and you have stated volumes cannot be committed and child containers do in fact inherit volume definitions from parents. 作为另一个答案,您已声明无法提交卷,子容器实际上从父级继承卷定义。 Hence any changes to the volume will be discarded. 因此,对卷的任何更改都将被丢弃。 I would like to add data such as mysql database files should always be in volumes for several reasons and you should not be trying to commit it into your image. 我想添加诸如mysql数据库文件之类的数据应该总是在卷中,原因有几个,你不应该尝试将它提交到你的图像中。
So what should you do instead? 那么你该怎么做呢?
You are probably better off rolling your own mysql. 你可能最好滚动你自己的mysql。 That way there are no surprises, no hidden "magic" in there. 这样就没有惊喜,没有隐藏的“魔法”。
If you look at an example dockerfile for mysql core, you can see the VOLUME declaration for /var/lib/mysql. 如果查看mysql核心的示例dockerfile ,可以看到/ var / lib / mysql的VOLUME声明。
Docs on docker volumes. Docker卷上的文档。
Data volumes 数据量
A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data: 数据卷是一个或多个容器中的特殊指定目录,它绕过Union文件系统为持久性或共享数据提供若干有用的功能:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.