MySQL Docker Container Does Not Save Data for New Image

I am trying to create a MySQL Docker container that is preconfigured with a specific schema and seed data so that I can connect other containers to it in the form of db. I use the dockerfile / mysql trusted file as the base, and I wrote a Docker file to create a new image from this base and add my .sql schema to it. After creating this image (mysql: base), I try to run bash in a new container, then go into mysql and create my database and then import the schema. Then I exit the container and try to pass the container to a new Docker sample. However, the resulting image does not save any changes that I made to MySQL db. It saves other files that I wrote in the container, but not db.

Here is the Docker file that I use to create the original image (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 that, I look into the image:

 docker run -i -t myorg/mysql:base bash 

And start MySQL to import the schema:

 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 enter mysql and verify that the schema has been imported successfully:

  mysql -u root -D mydb -e "SELECT * from tokens;" 

Also, if I go to /var/lib/mysql , I see that there is a mydb directory containing the .frm files corresponding to db.

But when I go out and try to pass this container to a new image:

 docker commit -m="import schema.sql" -a="Me" 72c2ff39dd65 myorg/mysql:seed 

And then go to the new image:

 docker run -i -t --rm myorg/mysql:seed bash 

The db files are no longer in / var / lib / mysql, and starting mysql -u root -e "SHOW DATABASES" does not show the mydb database, but only the default values mysql , information_schema and performance_schema dbs. I found that if I create a new text file in the container ( echo 'test' > newfile ), this file will be present in the captured image, but not in db.

I wonder if this is because the Dockerfile with the trusted image has VOLUME ["/etc/mysql", "/var/lib/mysql"] , so it mounts the db directory as the volume. My Dockerfile does not have this command, but I donโ€™t know if it inherited it anyway (I donโ€™t quite understand how the volumes work well enough to know how this might affect my build). I donโ€™t need db installed as one because I need another container to connect to it over a network connection (for this I use docker links).

FWIW, I am running boot2docker 1.3.2 on OS X 10.9.5.

+6
source share
2 answers

As another answer, you said that volumes cannot be committed, and child containers do indeed inherit volume definitions from their parents. Consequently, any changes in 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 try to fix it in your image.

  • If you need to transfer containers, there is no easy way to extract data from containers if it is not in the volume.
  • If you want to run several containers that exchange data, you need to have data in the volume.
  • If you want to change the container definition using new volumes or ports, you must delete and restore it. If you have data in the container, you will lose it. (See this question for an example of this case.)
  • A federated file system is slower than regular file systems, which will slow down your application or database.

So what should you do instead?

  • Use a data container that can be associated with any instance of the service container. Then you can use volumes to get data into the service container.
  • Use the volume installed on the host to restart the containers and install them in new containers.
+5
source

Most likely you are better off folding your own mysql. Thus, there are no surprises, there is no hidden "magic" there.

If you look at the dockerfile example for the mysql core, you can see the VOLUME declaration for / var / lib / mysql.

Documents on docker volumes.

Data volumes

A data volume is a specially designated directory in one or more containers that bypasses the Union file system to provide several useful functions for persistent or shared data:

  • Data volumes can be shared and reused between containers
  • Changes in the amount of data are made directly.
  • Changes to the data volume will not be included when updating the image
  • Volumes are maintained until containers are used.
+1
source

Source: https://habr.com/ru/post/979951/


All Articles