Table of Contents
In this article, i will take you through 7 Useful Docker Volume Command Examples. In a very simple way, Docker volume is a folder that exists on the Docker host and is mounted and accessible inside a running Docker container. The accessibility goes both ways, allowing the contents of that folder to be modified from inside the container, or on the Docker host where the folder lives.
Now, this description is a bit of a generalization. Using different volume drivers, the actual location of the folder being mounted as a volume can be hosted somewhere not on the Docker host. With volume drivers, you are able to create your volumes on remote hosts or cloud providers. For example, you can use an NFS driver to allow the creation of Docker volumes on a remote NFS server.
Some Features of Volume in Docker
a)Decoupling Container from Storage
b)Share Data/Storage among different Containers
c)Deleting Container does not delete volume
Docker Volume Commands
Also Read: Networking in Docker with Best Example
Example 1: Create a Volume
If you want to create a volume, you need to use docker volume create testvolume
command as shown below. You can see from the output testvolume
got created.
[root@localhost ~]# docker volume create testvolume testvolume
Example 2: List all the Volumes
If you want to list all the available volume in your system, you need to run docker volume ls
command. It will display all the current available volumes in your system as shown below.
[root@localhost ~]# docker volume ls DRIVER VOLUME NAME local testvolume
Example 3: Remove all the Volumes
If you want to remove all the volumes which is not used by any of the container, then you need to use docker volume prune
command as shown below. Here you can see a warning message as well to confirm if you want to remove all volumes not used by at least one container. This is to avoid any unwanted mistake.
[root@localhost ~]# docker volume prune WARNING! This will remove all volumes not used by at least one container. Are you sure you want to continue? [y/N] y Deleted Volumes: testvolume Total reclaimed space: 0 B
Example 4: Remove a Volume
If you want to remove any volume, let's say testvolume
in this case, then you need to run docker volume rm testvolume
command. It will remove testvolume
from your system as you can see below.
[root@localhost ~]# docker volume rm testvolume testvolume
Example 5: Remove Multiple Volumes
If you want to remove multiple volumes in one single command, then you can provide the names of those volumes with rm command as shown below. Here I am removing testvolume
and anothervolume
using docker remove rm testvolume anothervolume
command.
[root@localhost ~]# docker volume rm testvolume anothervolume testvolume anothervolume
Example 6: Inspect a Volume
If you want to inspect and verify for any error in your volume, then you can run docker volume inspect testvolume
command and check that as you can see below. Here you can see other information like mountpoint, name, scope etc.
[root@localhost ~]# docker volume inspect testvolume [ { "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/testvolume/_data", "Name": "testvolume", "Options": {}, "Scope": "local" } ]
Example 7: Check Other Docker Command Option
If you want to take any help for volume command with docker, you can use --help
option as you can see below and it will show you all the options available with the docker command.
[root@localhost ~]# docker volume --help Usage: docker volume COMMAND Manage volumes Options: --help Print usage Commands: create Create a volume inspect Display detailed information on one or more volumes ls List volumes prune Remove all unused volumes rm Remove one or more volumes Run 'docker volume COMMAND --help' for more information on a command.
Also Read: Install Docker on CentOS 7 with Thin Docker Provisioning
Reference: Docker Quick Start Guide