简体   繁体   English

MySQL Docker容器未将数据保存到新映像

[英]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数据库,只显示默认的mysqlinformation_schemaperformance_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数据库文件之类的数据应该总是在卷中,原因有几个,你不应该尝试将它提交到你的图像中。

  1. If you have to migrate containers there is no easy way to extract data from containers if its not in a volume. 如果必须迁移容器,则如果容器中的数据不在卷中,则没有简单的方法可以从容器中提取数据。
  2. If you want to start multiple containers that share data you have to have data in a volume. 如果要启动多个共享数据的容器,则必须在卷中包含数据。
  3. If you want to change your container definition with say new volumes or ports you have to delete and recreate it. 如果要使用新卷或端口更改容器定义,则必须删除并重新创建它。 If you have data in the container you will loose it. 如果容器中有数据,则会将其丢失。 (See this question for an example of this case.) (请参阅此问题以获取此案例。)
  4. The union file system is slower than normal file systems which will slow down your application or database. union文件系统比普通文件系统慢,这会降低应用程序或数据库的速度。

So what should you do instead? 那么你该怎么做呢?

  1. Use a data only container that can be linked with any instance of the service container. 使用可以与服务容器的任何实例链接的仅数据容器。 Then you can use volumes-from to get your data into the service container. 然后,您可以使用volumes-from将数据导入服务容器。
  2. Use a host mounted volume so that you can restart containers and mount the same location into new containers. 使用主机安装的卷,以便您可以重新启动容器并将相同的位置安装到新容器中。

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文件系统为持久性或共享数据提供若干有用的功能:

  • Data volumes can be shared and reused between containers 可以在容器之间共享和重用数据卷
  • Changes to a data volume are made directly 直接对数据卷进行更改
  • Changes to a data volume will not be included when you update an image 更新映像时,不会包括对数据卷的更改
  • Volumes persist until no containers use them 卷一直存在,直到没有容器使用它们

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

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