Docker - Volumes



Docker volumes are an important part of data management in Docker as they provide data persistence in Docker containers. Volumes allow data to exist outside of the container's writable layer, guaranteeing that the data is not lost during the removal or upgrade of containers. This becomes especially important for databases, configuration files, or application data that need to survive across container restarts or deployments.

Unlike bind mounts, which map directories from the host filesystem into the container, Docker volumes are managed by Docker itself and provide a higher abstraction and portability. Since volumes can be shared among multiple containers, they come in handy when different services need access to the same data.

How does a Docker Volume Work?

Docker volumes are independent file systems that exist outside a container's life cycle. Such a separation makes sure that data persists and that it can easily be shared between more than one container.

The way Docker volumes work is as follows −

  • Creation − Volumes can be explicitly created by using the docker volume create command, and it can also be done implicitly when a container is started with a volume mount.
  • Storage − Volumes are stored in a particular directory on the host, under /var/lib/docker/volumes by default on Linux.
  • Mounting − Volumes are mounted to specific paths in containers: data written to those paths are stored in the volume, not in the container's writable layer.
  • Persistence − Even stopping, updating, or deleting a container won't affect the data within the volume. Hence, this makes it relatively easy to have persistence across different instances of containers.
  • Sharing − Volumes can be shared by many containers simultaneously. It enables access to, or modification of, the very same data by different containers.
  • Management − You can easily manage volumes with Docker commands such as listing, inspecting, and removing them. Additionally, you can use volume drivers to store volumes on remote hosts or cloud providers.

Difference Between Volumes and Bind Mounts

Both volumes and bind mounts in Docker help you to store and share data across containers and host machines but in different manners. Lets discuss the key differences between Docker volumes and bind mounts.

Feature Docker Volumes Bind Mounts
Management Managed by Docker Managed by the host system
Location Stored in Docker's managed directory (/var/lib/docker/volumes/) Can be any directory on the host filesystem
Portability More portable, suitable for container orchestration Less portable, depends on the host's directory structure
Performance Optimized for I/O operations by Docker Performance depends on the host's filesystem
Isolation Provides better data isolation and security Direct access to the host's filesystem, less isolation
Ease of Use Simpler to use and manage through Docker commands Requires knowledge of host filesystem paths
Sharing Data Easily shared among multiple containers Can be shared, but with more complexity
Data Persistence Data persists beyond the container lifecycle Data also persists but is managed outside Docker's scope
Backup & Restore Easier to back up and restore using Docker commands Requires manual backup and restore processes
Flexibility Less flexible with fixed storage paths More flexible, allowing arbitrary host paths
Security Better security management within Docker's context Relies on the host's security settings and access controls
Configuration Created and managed with docker volume commands Specified directly in the docker run command
Setup Requires Docker to handle volume lifecycle Can be set up without Docker, using host paths
Permissions Managed by Docker with volume options Inherits permissions from the host's directory

Docker Volume Commands

As discussed, Docker volumes help us to create and manage persistent storage in Docker containers. Docker volume commands make it easier to manage them. Lets discuss a few of these commands used to create, manage, and perform various operations on Docker volumes

Creating a Docker Volume

If you want to create a new Docker volume, you can use the docker volume create command. The volumes created using this command can be used by one or more containers.

$ docker volume create my_volume
Docker Volumes 1

Listing Docker Volumes

Listing docker volumes in the local host machine will be one of the most frequently used commands that will help you manage volumes. If you want to list all the Docker volumes on your system, you can use the docker volume ls command.

$ docker volume ls
Docker Volumes 2

Inspecting a Docker Volume

When you want to fetch detailed information about a particular volume, you can use the docker volume inspect command. This command needs the name or id of the volume to be specified which you can get using the volume list command. It will provide details such as the volume's location on the host and its configuration.

$ docker volume inspect my_volume
Docker Volumes 3

Removing a Docker Volume

If you want to clean up your local machines and remove Docker volumes that are no longer required, you can use the docker volume rm command. It allows you to remove only those volumes that are not currently in use by any containers.

$ docker volume rm my_volume
Docker Volumes 4

Using a Docker Volume with a Container

When you run a container, you can specify the volume using the -v or --mount option. The -v option is simpler and more commonly used, while --mount provides more advanced configuration options.

Using -v

$ docker run -d -v my_volume:/data my_image
Docker Volumes 5

Using --mount

$ docker run -d --mount source=my_volume,target=/data my_image

Removing Unused Volumes

The Docker volume remove command only allows you to remove one volume at a time. But if you want to remove all unused Docker volumes to free up some space in your machine, you can use the docker volume prune command. When you run this command, it will prompt you for a confirmation before deleting all dangling volumes.

$ docker volume prune
Docker Volumes 6

Backing Up a Docker Volume

Its always useful to back up data stored in volumes because if you accidentally delete the volume, it will help you to restore the lost data back. You can store the data in the volumes into a tarball file. To do so, you can use a container to tar the volume's contents and output it to a file on the host system. Heres an example command for a volume called my_volume −

$ docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /data
Docker Volumes 7

Restoring a Docker Volume from Backup

Once you have your data backed up in a tarball file, you can use the below command to restore it back to a Docker volume. Heres the example command to restore a volume called my_volume from a backup file −

$ docker run --rm -v my_volume:/data -v $(pwd):/backup ubuntu tar xvf /backup/backup.tar -C /data
Docker Volumes 8

Conclusion

In this chapter, we have discussed what Docker volumes are and how they are different from bind mounts. Then we discussed the several Docker volume commands that will help you to easily manage and work with persistent storage in Docker environments. Using these commands, you can create, inspect, list, remove, back up, and restore volumes. This ensures that applications in your containers have reliable and flexible storage whenever needed.

FAQs on Docker Volumes

1. How do I persist data in Docker Containers using Volumes?

You can mount a Docker volume to any path inside the container and have data persisted across container life cycles. You can create a named volume using docker volume create and then mount it with either -v or the --mount flag when starting a new container.

2. Can I share Docker volumes between containers?

Yes, it is possible. Multiple containers can be granted access to the same volume and be able to change them. To share a volume, you just need to mount the same volume once to several different containers. Docker, by itself, synchronizes the data between the running instances.

Advertisements