top of page

Persistent data on Docker: Volumes

Updated: Aug 2, 2024



Docker is a very powerful tool in all fields concerning software, but this has a small price to pay: More complexity to the architecture.


One of the most important component of the Docker architecture are volumes.

Volumes are very crucial for a good reason:

Data persistence.

Well now everyone has the question, Docker or not Docker, data are saved right?!


Yes, of course they are saved, but the Docker container has a life cycle. If the container is turned off or destroyed for various reasons, all data saved inside that container disappears.

To manage the persistence or lifetime of data, Docker Volumes come into play.


Docker volumes are classified into three types:

  • Named volumes

  • Anonymous volumes

  • Bind volumes


It must be said that by default Docker does not share its volumes with the host.

The data is saved in dedicated folders managed by Docker.


However, to allow the host to access these volumes, you can use appropriate commands and configure Docker volumes to share folders.

Let’s analyse each type of volume together:


1 – Named volumes

Named volumes are used for persistent data so even if the container is stopped or even deleted this data is still present.


A practical example would be a candidate recruitment platform. Candidates register and upload their CV. These CVs must persist even when the container hosting this web platform is destroyed and restarted for some reason.


Another example is Relational Database software, such as the famous PostgreSQL or MySQL. Those software products are available with Docker image which we can use in our projects.


Guess what kind of volumes are used?!

Named volumes.

Here is a practical example how we configure these types of volumes:

# -v: the option to configure a volume
docker run -d -it -v volume_name:/data/folder/inside/docker  --name container_name image_name .

2 – Anonymous volumes

These are volumes without a name which have a short life cycle. These volumes live as long as the container.

If the container is destroyed/removed these volumes are also destroyed.

If there are more than one container using the same anonymous volume, the data will persist until the last container is destroyed.

In short, if these anonymous volumes are no longer used by any container then they are automatically destroyed.

They are usually used for temporary data, logs, and you don’t care about a volume name.

Here is a practical example how we configure these types of volumes:

# -v: the option to configure a volume 
docker run -d -it -v /data/folder/inside/docker --name container_name image_name . 

3 – Bind volumes

These volumes are very important and are also the most complicated to understand at the beginning for those entering inside the world of docker.

These volumes are persistent and have a peculiarity:

They can be shared with the host.

This means that every change/addition of files inside the docker container is reflected to the host and vice versa.


Here is a practical example how we configure these types of volumes:

# -v: the option to configure a volume
docker run -d -it -v /data/folder/host:/data/folder/inside/docker  --name container_name image_name . 

Another very important aspect is that these volumes help a lot during the testing and quality assurance phase.

During the testing and quality assurance phase , many CI/CD cycles are performed. This means that at every cycle you have to build the image and restart the containers but if we use these volumes how can we speed up the process?

Conclusion:

Volumes are very important to ensure persistence or life of the data, especially in production environment.

cipiklevis@gmail.com

+355674907376

  • White LinkedIn Icon
  • White Twitter Icon

©2024 by Klevis Cipi.

Albania, Tirane

bottom of page