Docker volumes are specially-designated directories within one or more containers that bypass the Union File System. In other words, a volume is used to keep persistent (non-immutable data) outside of the container. Volumes essentially outlive the container, which means that they require manual deletion to remove.
https://docs.docker.com/engine/reference/commandline/volume_ls/
To show all of the volumes on your host system, use:
docker volume ls
https://docs.docker.com/engine/reference/commandline/volume_inspect/
To show the detailed information about one or more volumes:
docker volume inspect {volume_id}
https://docs.docker.com/engine/reference/commandline/volume_create/
In the very rare occasion that you need to create a volume detached from a container, you can manually create a volume using:
docker volume create
You can specify a name, label, and a driver using the following flags:
docker volume create --name {name} --label {label} --driver {driver}
When using the --volume
or -v
flag in a run command, a new volume is created using the following format:
docker container run -v {volume_name}:{container_path} {image}
The {container_path}
refers to the path where the file or directory are mounted in the container. Essentially, this is the path to the persistent data within the container.
https://docs.docker.com/engine/reference/commandline/volume_rm/
To remove a volume:
docker volume rm {volume_id}
You may need to copy and paste the entire volume id for this command.
Bind Mounts are like volumes, but they actually refer to physical file locations on the host computer’s file system that are mounted onto the containers. This is opposed to volumes, where the persistent data is stored in Docker’s internal storage directories that are not meant to be accessed outside of Docker. This is essential for development environments where the application is subject to change.
There are no discrete commands for using bind mounts, but they can be created and accessed using the volume commands with a slightly different syntax.
Containers can be ran with a mounted volume using --volume
or -v
. Bind mounts are created using the same flags, but the first parameter refers to the path on the host machine instead of a name for the volume:
// Volume
docker container -v {volume_name}:{container_path} {image}
// Bind Mount (Mac/Linux)
docker container -v /{local_path}:{container_path} {image}
// Bind Mount (Win)
docker container -v //{local_path}:{container_path} {image}
Notice that the local path for Windows must have two forward slashes. Even though the Windows NTFS uses back slashes, in this command, forward slashes must be used in the path.
Since Docker 17.06, the --mount
flag is available for running containers. The syntax is longer, but also more explicit and verbose.
docker container run --mount '{key}={value},...' {image}
The following lists the available keys and values:
Key | Description |
---|---|
type | volume , bind , or tmpfs |
source (src) | If type=volume , this specifies the name |
destination (dst or target) | The path to the directory in the container |
readonly | If this is present, the bind mount is mounted into the container as read-only |
volume-opt | More mounting options with the syntax:volume-opt={option}=<key>:<value> |