HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqldockerMinor

Persistent data in MySQL with Docker

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
withdockerpersistentmysqldata

Problem

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.

Solution

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

Code Snippets

Removing intermediate container b3ff83bbf273
docker run -d -v /var/container_data/mysql:/var/lib/mysql mysql_image

Context

StackExchange DevOps Q#3483, answer score: 2

Revisions (0)

No revisions yet.