Managing Data in Containers Using Docker Volumes

Docker volumes are very interesting. It started as a solution to storing persistent data on top of the read-only Docker containers. The way Docker volumes are implemented and used, however, is nothing short of fascinating. In this article, we are going to discuss how volumes work and how you can take advantage of Docker volumes to expand the functionalities of your Docker apps.

Docker Volume

Docker Filesystem and How They Work

Before we go ahead and talk about Docker volumes, we must first understand how Docker containers work. Docker containers are arranged as read-only layers due to the nature of how filesystem works in them. Instead of modifying the stored files directly, Docker automatically creates a read-write layer on top of the application when the container is loaded.

The changes made to the read-write layer are not implemented to the underlying read-only structure of the Docker container. As long as the Docker container is running, changes are kept and you can always access the last state of the app. When you delete the Docker container and deploy it as a new image, however, you automatically revert back to the state of the read-only layer within the container.

This type of implementation is called Union File System. It is one of the primary strengths of Docker and offers a lot of advantages. You can, for example, start fresh with a Docker container at any given time. It is also possible to duplicate an entire Docker container and work on updates or changes without taking the deployed container offline.

Although this type of implementation offers the most flexibility and maximum compatibility, it is not without its challenges. Docker needs a way to store persistent data, mainly data that are critical to the application itself. It also needs a way to share data between containers without sacrificing flexibility along the way. This is where Docker volumes come in as a solution.

Docker Volumes for Storing Data

Docker volumes – or simply volumes – are directories or files that are stored outside of the core filesystem of Docker containers. It is running alongside the Union File System, allowing Docker containers to store data and keep the information even when the actual container is deleted or redeployed.

Docker volumes are not separate containers. They are simply a mount of the directory on the host. This brings us to the first benefit of using Docker volumes: the ability to use the same data folder across all of your containers.

The same directory can be mounted on multiple Docker containers as its own volume. You can then access the same persistent data from any running container that has the volume mounted. When you need to share information across Docker containers or have different parts of the same web application running on different containers, Docker volumes can be very handy nonetheless.

Another benefit you will get from using Docker volumes comes from the way volumes are designed. The actual folders or files on your host are also accessible even when you are not using Docker containers. Doing data maintenance, backing up the necessary information and performing other data maintenance tasks are so much easier to do this way.

Persistent and Real-Time

Changes to a data volume are made permanent. As mentioned before, the data stored inside a volume remains safe even when you choose to destroy the Docker container. The same goes for when you update your Docker image. All you have to do to access the stored data is mount the Docker volume again.

You can add a data volume using one of these two methods:

– You can add a Docker volume using the –v flag when performing docker create or docker run command.

– You can let a Docker container access data from other containers using the –volumes-from command when running the docker run command.

Both methods work well in different situations. Which one should you use? The answer to this question depends highly on how your web application and Docker containers are structured.

Docker Volumes Usage

Now that we know how useful Docker volumes can be, it is time to take a look at some of the common ways of using Docker volumes to support web applications. The most common way of using Docker volumes is in a data-only container. You can set up a dedicated Docker container for storing information and persistent data, mount a specific Docker volume and share access to it. Other Docker containers can then use the volume to store information as well.

Volumes can also be used to store backups. It is easy to create a routine that will back up the data container or data from other Docker containers to a specific volume. Since volumes are just mounted directories on your host, it is also possible to have the backup files synced to remote servers or downloaded to an offline disk for better safekeeping.