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.
Docker has two options for containers to store files in the host machine so that the files are persisted even after the container stops:
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.
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.
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.