This assumes you have installed Docker on Linux and Docker is running. The following command creates a docker volume named foo-volume.
docker volume create foo-volume
The docker volume ls command can be used to display the docker volumes.
docker volume ls
Which should return something like this.
DRIVER VOLUME NAME
local foo-volume
Or, as a more practical example, here is now to create a volume that points to an Amazon Web Services Elastic File System (EFS).
sudo docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=172.31.16.17,nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport \
--opt device=:/ \
efs
The docker volume inspect command can be used to display the configuration of the volume.
docker volume inspect foo-volume
Which should return JSON something like this. Notice in this example that mountpoint is /var/lib/docker/volumes/foo-volume/_data. This correlates to a directory on the host operating system.
[
{
"Driver":"local",
"Labels":{},
"Mountpoint":"/var/lib/docker/volumes/foo-volume/_data",
"Name":"foo-volume",
"Options":{},
"Scope":"local"
}
]
When creating a container using the docker run command, the -v or --volume option can be used to use foo-volume. Do not be thrown off by /usr/local/docker/foo at the end of this command. This is simply the directory that contains the Dockerfile used to create the container.
docker run --volume foo-volume:/app/work /usr/local/docker/foo
Three colon separated values are used with the -v or --volume command line option.
- The name of a Docker volume or the absolute directory to where data will be stored on the Docker system.
- The path to where the volume will be mounted in the container.
- Optional - Comma separated permissions for the mount, such as ro (read only)
In this example, the /var/lib/docker/volumes/foo-volume/_data (foo-volume) directory on the Docker system will be mounted to the /app/work directory in the container.
Did you find this article helpful?
If so, consider buying me a coffee over at