Docker Volumes: A Comprehensive Introduction

Docker volumes are very useful when we need to persist data in Docker containers or share data between containers.

Docker volumes are important because when a Docker container is destroyed, it’s entire file system is destroyed too. So if we want to keep this data, it is necessary that we use Docker volumes.

Docker volumes are attached to containers during a docker run command by using the -v flag

When Docker Volumes are needed

Let’s look at the docker image we created in a previous article. The stephenafamo/adonisjs image.

The working directory is /var/www, and the application code is there. If we want to run an Adonis application in a container from this image and we don’t want all our files to be deleted once we stop the container, then we have several choices.

1. Bind mounts

A bind mount is just mapping a directory on the host machine to a directory in the container. However, when the container is removed, it does not affect the directory.

If the -v or --volume flag’s value is a path, then it is assumed to be a bind mount. If the directory does not exist, then it will be created. When we do this, the directory /path/to/app/directory will contain the same files as /var/www in the container.

NOTE: If you are confused about how to use the docker run command or how to run containers, then you can go through the the previous article.

2. Docker volumes

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:

Volumes are easier to back up or migrate than bind mounts.

You can manage volumes using Docker CLI commands or the Docker API.

Volumes work on both Linux and Windows containers.

Volumes can be more safely shared among multiple containers.

Volume drivers allow you to store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.

A new volume’s contents can be pre-populated by a container.

In addition, volumes are often a better choice than persisting data in a container’s writable layer, because using a volume does not increase the size of containers using it, and the volume’s contents exist outside the lifecycle of a given container.

Creating and Removing a Docker volume

We can create a Docker volume using the command

docker volume create volume_name

To list all the volumes, we use the following command.

docker volume ls

docker volume ls

If we ever choose to delete a Docker volume, we do it using the command.

docker volume rm volume_name

Attaching a Docker volume to a container

We can also use the -v or --volume flag to attach a Docker volume to a container. However, instead of putting the path to the directory on the host machine as we did with bind mounts, we simply put the volume name.

Conclusion

So, this covers the common uses of Docker volumes. There are also several volume drivers that allow for some more functionality. Perhaps we’ll talk about that in another article, or maybe I’ll update this later.

As always, I appreciate comments, suggestions, and corrections. Thank you.