Docker 101 : Volumes (Storage)

The container's data:

The docker container has its own filesystem that is independent from the host's filesystem.

After the installation, Docker creates directories where it stores images, log files,...

The container's data is tied to the container's life span, the data exists as long as the container is running , once we stop the container all the data related to it is lost.

We will need to map the container's directory to a directory on the host so the data persists on the host after the container is "destroyed". 

Docker images:

Docker images are created in layers as you can see below.

After you run the image inside the container, docker creates a Read-write copy of the parts the he needs to change in the Read-Only layers using the Copy-On-Write method.

The layered structures of the images allows for lot of flexibility. For example if we download an image that has a layer in common with an already downloaded image, docker doesn't download it.

Container creation:

Containers are a running a Read-Write version of the docker images.

The dockerfile contains a set of instructions on how to build the different layers of the an image.

We build an image using the below command:

The layers are the result of the dockerfile instruction. 

Each layer could be anything from compiled packages, to created directories, to updated packages.

All the layer are Read-Only

Docker directories:

When we install docker, it creates a structure similar to the one below on the host:

Docker root directory is /var/lib/docker and under it there are directories for storing images, volumes(storage directory), ...

From read-only to read-write image:

In order to create a Read-Write image in top of the Read-Only one, we use the below command:

This Read-Write image is able to store data related to the container (data, log file,...)

When the container is stopped the Read-Write image is removed.

Persistent storage:

To be able to keep the container data after the container is stopped or destroyed, we create a directory Vol_A on the host under the directory /var/lib/docker/volumes :

We check if the volume has been created:

Then we can map Vol_A to a directory on the host machine:

Docker uses the default directory /var/lib/docker/volumes/Vol_A.

If we want to store the container data in another folder we should specify its full path name.

We can use an alternative command to the one above:

Mount types:

volume : mounts the directory from the default location /var/lib/volumes.

bind : (as in the example above) mounts the directory from other locations.


In the example above, the container directory /var/lib/web_data is mapped to the host directory /backup .


Leave as a comment: