Docker Storage :
In this section we will go through the concept of how docker stores data and how it manages file system of the containers.
File system :
When we install docker it will create a folder structure as /var/lib/docker. We have multiple folder inside it like containers, image, volumes etc. This is where docker stores its data.
Layered Architecture
Using Layered Architecture docker build images very fast and efficiently. Docker will pull and install the images only once and from second times onwards when we try to run the same image it will take them from cache.
In case of rebuild and updates also it only rebuild by updating the latest source code. Which save a lot of time.
All the modification will be done on the file of container Layer.
Until we rebuild the image using docker build command the files in Image Layer will not be changed.
If the container stops or removed then all the data with-in will be lost.
volumes :
volume helps to persist the data even it container got stopped or deleted.
volume can be created separately and attached with image. This is called the volume mounting. Even if volume is not created before the docker run command, it will be created at run time.
# create docker volume :
docker volume create data_volume
docker run -v data_volume:/var/lib/mysql mysql
The first command will create a data_volume and the second command will create a new container and mount this data_volume to /var/lib/mysql. All the data created by database will also be stored in data_volume. If the container get destroyed we will still have the data.
Mounting Data from external folder in host system : bind Mounting
volume mount mounts a volume from the volumes directory and bind mount mounts a directory from any location on the docker host.
# run with -v to create a volume at run time
docker run -v /data/mysql:/var/lib/mysql mysql
We should use --mount option instead of -v. Its more verbose and use key-value pair for easy understading.
# -- mount
docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql
Storage drivers
Docker uses Storage driver to enable layered-architecture. Mainly Storage driver is responsible for doing all the things like maintainance of layered-architecture, creating a writable layer, moving files across layers and enable copy and write etc..
Depending upon the underlying Operating System default storage driver will be selected automatically.
# Common Storage Drivers
AUFS
ZFS
BTRFS
Device Maper
Overlay
Overlay2