2

If I startup MySQL server during docker build (derived from official MySQL Dokcer Image) in two different statements, would be database files be pertained?

I would say yes, but it seems to be wrong. Why?

Logfile:

180228 08:54:50 mysqld_safe Logging to '/var/lib/mysql/c2712433a4da.err'.
180228 08:54:50 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
Tables_in_FOOBAR
address
category
Removing intermediate container b3ff83bbf273
 ---> 0b366a0d31a9
Step 9/9 : RUN chmod 777 /var/lib/ -R && ./ingest_data.sh 
 ---> Running in 149361f7d465
180228 08:55:00 mysqld_safe Logging to '/var/lib/mysql/c2712433a4da.err'.
180228 08:55:00 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Database
information_schema
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000): Unknown database 'FOOBAR'

As you can see, in the first RUN statement I can list tables I have created in a new database, but in the second RUN statement the database is gone.

Note. Normally, you would not transport your application data in production containers (immutability principle) but for demo and test purposes this can be nevertheless useful.

2

That's a known issue .. since image layers are built using containers, data saved to folders does not make it to the next layer or your final container

Removing intermediate container b3ff83bbf273

If you want data to persist between updated containers you have to manually map data to the host outside the container-specific mount points.

For example, when creating a container use the -v option to map a volume:

docker run -d -v /var/container_data/mysql:/var/lib/mysql mysql_image

For a detailed documentation https://docs.docker.com/engine/reference/run/#volume-shared-filesystems

2
  • but why packages from apt-get install .. make it to the final image? Mar 2 '18 at 8:15
  • 1
    Because RUN apt-get install .. will create an intermediate container , run commands then commit it to the final image. In your example, first you're starting MySQL server creating data then commiting to the final image but then running a second server with a new intermediate container and empty database.
    – storm
    Mar 2 '18 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.