1. Overview

Docker is one of the most popular container technologies. It packages an application with its dependencies and allows it to run in an isolated environment. This technique greatly increases an application’s portability. By default, container storage is ephemeral, which is not ideal for stateful applications. However, we can use volumes to overcome this limitation.

In this tutorial, we’ll see how to list volumes and show detailed information about them.

2. Setting up an Example

Let’s create a few volumes with attributes to use as an example:

$ docker volume create dangling-volume
$ docker volume create narendra-volume --driver local --opt type=tmpfs --opt device=tmpfs
$ docker volume create labeled-volume --label owner=narendra

The latter section of the tutorial shows how we can use these attributes to filter volumes. In the next section, we’ll see how to verify our example has been set up correctly.

3. Displaying Basic Volume Information

Docker’s volume list child command will display a brief summary of each volume:

$ docker volume list
DRIVER    VOLUME NAME
local     dangling-volume
local     labeled-volume
local     narendra-volume

Sometimes, we only need the volume names. In such scenarios, we can use the –quiet option:

$ docker volume list --quiet
dangling-volume
labeled-volume
narendra-volume

The above approaches work fine for a small handful of volumes but can be tedious when there are many volumes to list. In such cases, we can use the –filter option. This option allows us to perform filtering based on certain attributes. Let’s look at a few examples.

3.1. Filtering on Name

We can use the name filter to list the volume whose name contains a certain string. Let’s list the volume whose name contains “narendra”:

$ docker volume list --filter name=narendra
DRIVER    VOLUME NAME
local     narendra-volume

3.2. Filtering on Labels

Labels are used to tag resources. A very common scenario is to group the resources that match certain criteria. For example, developers can use their usernames as a label so they can easily identify the volumes they’ve created themselves. Let’s understand this with an example:

$ docker volume list --filter label=owner=narendra
DRIVER    VOLUME NAME
local     labeled-volume

In this example, the filter matches volumes with the label owner=narendra, which we added when setting up our example volumes.

3.3. Filtering on the Driver’s Name

Sometimes, we need to segregate volumes based on their driver’s name. In such cases, we can use the driver filter:

$ docker volume list --filter driver=local
DRIVER    VOLUME NAME
local     dangling-volume
local     labeled-volume
local     narendra-volume

3.4. Dangling Volumes

Volumes take up space on the docker host, so it’s a good practice to clean up any unused volumes. Because removing the wrong volume can cause data loss, we have to be extra careful before deleting them. As a safety check, we can ensure a volume is not referenced by any container by using the dangling filter. Let’s see this in action.

First, let’s create a container that uses volumes:

$ docker container run -d --name dangling-volume-demo -v narendra-volume:/tmpwork \
   -v labeled-volume:/data busybox
fa3f6fd8261293a92da7efbca4b04040a1838cf57b2703795324eb70a3d84143

In this example, the container is using two of our three volumes: narendra-volume and labeled-volume. Let’s now confirm that the third volume is the only one that appears as dangling/unused:

$ docker volume list --filter dangling=true
DRIVER    VOLUME NAME
local     dangling-volume

4. Displaying Detailed Volume Information

The list child command shows very limited information about volumes. Sometimes this is not enough. For example, debugging is much easier if we know the details of a volume. In such cases, we can use the volume inspect child command to get additional information about a volume. This command shows information like the volume’s creation timestamp, mount point, etc. Let’s see this with an example:

$ docker volume inspect labeled-volume
[
    {
        "CreatedAt": "2022-05-30T22:34:53+05:30",
        "Driver": "local",
        "Labels": {
            "owner": "narendra"
        },
        "Mountpoint": "/var/lib/docker/volumes/labeled-volume/_data",
        "Name": "labeled-volume",
        "Options": {},
        "Scope": "local"
    }
]

5. Displaying Container-Specific Volume Information

Another very common scenario is to find the volumes used by a given container. Developers often need this information to debug the application. We can get information about a specific contain’s volume(s) using the container inspect child command. This command returns low-level information on Docker objects, such as their state, host configuration, network setting, etc. We can specify the Mounts section to gather mount information about a volume:

$ docker container inspect --format '{{ json .Mounts }}' dangling-volume-demo | python3 -m json.tool
[
    {
        "Type": "volume",
        "Name": "narendra-volume",
        "Source": "/var/lib/docker/volumes/narendra-volume/_data",
        "Destination": "/tmpwork",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    },
    {
        "Type": "volume",
        "Name": "labeled-volume",
        "Source": "/var/lib/docker/volumes/labeled-volume/_data",
        "Destination": "/data",
        "Driver": "local",
        "Mode": "z",
        "RW": true,
        "Propagation": ""
    }
]

Note that, in this example, we’ve piped output to the Python interpreter in order to make the JSON output easier to read. However, this is completely optional.

6. Conclusion

In this article, we saw some practical examples of listing docker volumes. First, we used the list child command. Then we saw how to use filters with it. Finally, we used the inspect child command to show detailed information about the volumes.

Comments are closed on this article!