<p>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</p>

<pre><code>Removing intermediate container b3ff83bbf273
</code></pre>

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

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

<pre><code>docker run -d -v /var/container_data/mysql:/var/lib/mysql mysql_image
</code></pre>

<p>For a detailed documentation 
<a href="https://docs.docker.com/engine/reference/run/#volume-shared-filesystems" rel="nofollow noreferrer">https://docs.docker.com/engine/reference/run/#volume-shared-filesystems</a></p>

CC BY-SA 3.0
2018-03-02T08:15:28.913Z
but why packages from `apt-get install ..` make it to the final image?
Ta Mu
CC BY-SA 3.0
2018-03-02T09:48:54.057Z
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
CC BY-SA 3.0
2018-02-28T13:37:56.083Z
2018-02-28T13:37:56.083Z
storm
<p>If I startup MySQL server during <code>docker build</code> (derived from official MySQL Dokcer Image) in two different statements, would be database files be pertained?</p>

<p>I would say yes, but it seems to be wrong. Why?</p>

<p>Logfile:</p>

<pre><code>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
 ---&gt; 0b366a0d31a9
Step 9/9 : RUN chmod 777 /var/lib/ -R &amp;&amp; ./ingest_data.sh 
 ---&gt; 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'
</code></pre>

<p>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.</p>

<p>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.</p>

<docker><mysql>
Persistent data in MySQL with Docker
Ta Mu
# Persistent data in MySQL with Docker

(Asked by: username_0 on 2018-02-28)

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.

**Tags:** docker, mysql


---

## 1 answers

### Answer by username_1 on 2018-02-28. Score: 2 | Accepted answer

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

> **Comments:**
> **username_0**: but why packages from `apt-get install ..` make it to the final image?  
> **username_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.  

---

