Docker Storage
Last updated
Last updated
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
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
The new method of is use --mount. Example (see above).