# Docker Storage

How docker store data on the local file system? When you install docker on a system, it creates this folder structure at directory: var/lib/docker

![](https://2299818979-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LnQNlsYsgke3L_g2vbZ%2F-MAR4V43PLZsLxbKExr8%2F-MAR4ys259ll1YztfudH%2F2.png?alt=media\&token=b3057cd0-9361-4379-95d5-0e8f6adaba3d)

When we run docker build, we are building image layers to form final docker image and these image layers are read only. You can modify them only by initiating a new build.

When you run a container, docker create a container based on these image layers and create a new writable layer (read-write) on top of image layers.

The writable layer is used to store data created by the container such as log file or temp file generated by the container or any file modified by user on the container. The life of this layer is as long as the container is alive.

What happen if I wish to modify the source code in the Read-Only after running a container? Remember that the same image layer may be shared between container. Does it mean I cannot modify the files inside the container? No, because I can still modify the file, but before I save the modified file, docker automatically create a copy of the file in the read write layer and I will be modifying different version of the file in the read-write layer. All future modification will be done on this copy of the file in the read-write layer. This called copy-on-write mechanism. So the modification does not happen in the read-only layer and thus image will remain the same all the time until you rebuild the image.

For persistence, you run **docker volume create data\_volume** and this create a folder called data\_volume under /var/lib/docker. When you run command, **docker run -v data\_volume:/car/lib/mysql mysql** I could mount this volume inside the docker container read/write layer.

**Volume Mounting**

Mount volume from volume directory. So, it means even though you do not run the command **docker volume create data\_volume2,** when you run **docker run -v data\_volume2:/car/lib/mysql mysql,** docker will automatically create data\_volume2 folder.

**Bind Mounting**

Mount a directory from any location on the docker host. For example, you have data at external storage on the docker host and we would like to store databases data on that volume and not in the default var/lib/docker volume folder. We can use **docker run -v /data/mysql:var/lib/mysql mysql**&#x20;

The new method of is use --mount. Example (see above).
