What is Docker Volume?
Docker volumes are a widely used and useful tool for ensuring data persistence while working in containers. Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.
- The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
- A container’s writable layer is tightly coupled to the host machine where the container is running. The data cannot be easily moveable somewhere else.
- Writing into a container’s writable layer requires a storage driver to manage the filesystem.
Docker has two options for containers to store files in the host machine so that the files are persisted even after the container stops:
- Volumes are stored in a part of the host filesystem, which is managed by
- Bind mountsmay be stored anywhere on the host system.
The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.
Types of Mount
In all types of mounts, the data looks the same from within the container. It is exposed as either a directory or an individual file in the container’s filesystem.
1. Volumes:Volumes are stored in a part of the host filesystem, managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker. We can create a volume explicitly using the docker volume create command, or Docker can create a volume during container or service creation.
When we create a volume, it is stored within a directory on the Docker host. Volumes are managed by Docker and are isolated from the core functionality of the host machine.
A given volume can be mounted into multiple containers simultaneously. When no running container uses a volume, the volume is still available to Docker and not removed automatically. But we can remove unused volumes using Docker volume prune.
When we mount a volume, it may be named or anonymous. Anonymous volumes are not given an explicit name when they are first mounted into a container, so Docker gives them a random name guaranteed to be unique within a given Docker host. Besides the name, named and anonymous volumes behave in the same ways.
Volumes also support volume drivers, allowing storing the data on remote hosts or cloud providers, among other possibilities.
2. Bind mounts: Bind mounts may be stored anywhere on the host system. They may be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time. Bind mounts have limited functionality compared to volumes.
When we use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full path on the host machine. The file or directory does not need to exist on the Docker host already. It is created on-demand if it does not yet exist.
Bind mounts are very efficient, but they rely on the host machine’s filesystem, having a specific directory structure available. If we want to develop new Docker applications, then consider using named volumes instead of bind mounts. We can’t use Docker CLI commands to directly manage bind mounts.
3. tmpfsmounts: tmpfs mounts are stored in the host system’s memory only and are never written to the host system’s filesystem. It is not persisted on disk, either on the Docker host or within a container.
tmpfs mount can be used during the container’s lifetime to store non-persistent state or sensitive information.
4. named pipes: An named pipe mount can be used for communication between the Docker host and a container. The common use case is to run a third-party tool inside a container and connect to the Docker Engine API using a named pipe.
Bind mounts and volumes can both be mounted into containers using the -v or –volume flag, but the syntax for each is slightly different.
In Docker 17.06 and higher, we use the –mount flag for both containers and services, for bind mounts, volumes, or tmpfs mounts.
-v or –mount flag
The -v or –volume flag was used for standalone containers, and the –mount flag was used for swarm services. However, starting with Docker 17.06, we can also use –mount with standalone containers.
In general, –mount is more explicit and verbose. The biggest difference is that the -v syntax combines all the options in one field, while the –mount syntax separates them.
New users should try –mount syntax because it is simpler than –volume syntax. And if the user needs to specify volume driver options, then use –mount.
1. -v or –volume: It consists of three fields, separated by colon characters (:). The fields must be in the correct order, but each field’s meaning is not directly understandable.
- In the case of named volumes, the first field is the name of the volume, and it is unique on a given host machine. For anonymous volumes, the first field is omitted.
- The second field is the path where the file or directory is mounted in the container.
- The third field is an optional and comma-separated list of options.
2. –mount: It consists of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> The –mountsyntax is more verbose than -v or -volume. The order of the keys is not significant, but the flag’s value is easier to understand.
- The typeof the mount, which can be volume, bind, or tmpfs.
- The destination takes as its value the path where the file or directory is mounted in the container. It may be specified as the destination, dst, or target.
- If the read-onlyoption present causes the bind mount to be mounted into the container as read-only.
- The volume-optoption, which can be specified more than once, takes a key-value pair consisting of the option name and its value.
How to Create and Manage Volumes
Create a volume
Use the following command to create and manage Docker volumes outside the scope of any container.
Docker automatically creates a directory for the volume on the host under the /var/lib/docker/volume/path.
Now mount this volume on a container, ensuring data persistence and data sharing among multiple containers.
List the volumes
Use the following command to list the volumes.
The output displays a list of volumes, specifying their location and their volume name.
Inspect a volume
Use the following command to inspect a volume.
It lists all the details of a volume, including its location on the host file (mountpoint), and everything stored within the data volume can also be found in the directory listed under the mountpoint path.
Mounting a Data Volume
To mount a data volume to a container, adds the –mount flag in the docker run command. It adds the volume to the specified container, storing the data produced inside the virtual environment. Use the following syntax to run a container and mount a data volume to it.
Everything stored in that directory automatically saved on the data volume on the host as well.
Remove a volume
To delete a Docker volume, we need to specify its name. Use the following basic command syntax to remove a volume.
Docker removes volumes only if they are not in use at the moment. If there is a container with the specified volume, it responds with an error.
Delete All Volumes at Once
Use the below command to delete all unused Docker volumes at once:
The output warns once that it will remove all local volumes not used by at least one container, and then we need to confirm to continue.