How to use docker volume ls command to list and filter volumes

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker volume ls command to manage your Docker volumes. We will begin by creating sample volumes to work with. Then, you will discover how to list all existing volumes on your system.

Building upon listing, you will explore how to filter volumes based on criteria such as name and driver, and also by their dangling status. Finally, you will learn how to format the output of the docker volume ls command for better readability and specific information extraction.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") subgraph Lab Skills docker/ls -.-> lab-555260{{"How to use docker volume ls command to list and filter volumes"}} docker/rm -.-> lab-555260{{"How to use docker volume ls command to list and filter volumes"}} docker/create -.-> lab-555260{{"How to use docker volume ls command to list and filter volumes"}} docker/volume -.-> lab-555260{{"How to use docker volume ls command to list and filter volumes"}} end

Create sample volumes

In this step, we will learn how to create Docker volumes. Volumes are the preferred way to persist data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.

To create a volume, we use the docker volume create command. Let's create a simple volume named myvolume.

docker volume create myvolume

You should see the name of the volume printed to the console if the creation was successful.

Now, let's create another volume named another_volume.

docker volume create another_volume

We have now created two volumes that can be used by Docker containers to store persistent data.

List all volumes

In this step, we will learn how to list all the Docker volumes that exist on our system. This is useful for seeing what volumes are available and managing them.

To list all volumes, we use the docker volume ls command. This command will show you a list of all volumes, including their driver and name.

docker volume ls

You should see the volumes we created in the previous step (myvolume and another_volume) listed in the output, along with any other volumes that might exist on the system. The output typically includes the DRIVER and VOLUME NAME.

The default driver for volumes is usually local, which means the volume is stored on the local machine where Docker is running.

Filter volumes by name and driver

In this step, we will learn how to filter the list of Docker volumes based on their name and driver. This is helpful when you have many volumes and want to find specific ones.

We can use the --filter flag with the docker volume ls command to filter the output. The filter takes a key=value pair.

To filter by name, we use the name key. Let's filter for the volume named myvolume.

docker volume ls --filter name=myvolume

You should only see the myvolume listed in the output.

Now, let's filter by the driver. Since we used the default driver when creating the volumes, the driver is local. Let's filter for volumes using the local driver.

docker volume ls --filter driver=local

This command should list all volumes that use the local driver, which should include both myvolume and another_volume.

You can also combine filters. For example, to filter for a volume named myvolume that uses the local driver, you can use:

docker volume ls --filter name=myvolume --filter driver=local

This will again show only the myvolume.

Filter volumes by dangling status

In this step, we will learn how to filter Docker volumes based on their "dangling" status. A dangling volume is a volume that is not currently attached to any container. These volumes can consume disk space unnecessarily.

To filter for dangling volumes, we use the --filter dangling=true flag.

First, let's create a container and attach one of our volumes to it. We will use the ubuntu image. If you don't have the ubuntu image locally, Docker will pull it automatically.

docker run -d --name mycontainer -v myvolume:/app ubuntu sleep 3600

This command runs a container named mycontainer in detached mode (-d), mounts myvolume to the /app directory inside the container (-v myvolume:/app), uses the ubuntu image, and keeps the container running for an hour (sleep 3600).

Now, let's list all volumes again:

docker volume ls

You will see both myvolume and another_volume. myvolume is currently in use by mycontainer. another_volume is not attached to any container, so it is a dangling volume.

Now, let's filter for dangling volumes:

docker volume ls --filter dangling=true

You should see another_volume listed in the output, as it is not attached to any running container.

To see volumes that are not dangling (i.e., attached to a container), you can use --filter dangling=false.

docker volume ls --filter dangling=false

This should show myvolume.

Finally, let's stop and remove the container we created.

docker stop mycontainer
docker rm mycontainer

Now, if you list dangling volumes again:

docker volume ls --filter dangling=true

Both myvolume and another_volume should now appear as dangling, because myvolume is no longer attached to a container.

Format volume output

In this step, we will learn how to format the output of the docker volume ls command. This allows us to customize the information displayed and present it in a way that is most useful for our needs.

We can use the --format flag to specify the output format. This flag accepts Go template syntax.

Let's list the volumes and only display the volume name. We can use the {{.Name}} template to achieve this.

docker volume ls --format "{{.Name}}"

This command will output a list of volume names, one per line.

Now, let's display both the driver and the name, separated by a colon. We can use {{.Driver}}:{{.Name}}.

docker volume ls --format "{{.Driver}}:{{.Name}}"

The output will show the driver and name for each volume, like local:myvolume.

We can also use the table format to display the output in a more readable table format, similar to the default output but with more control over the columns. Let's display the driver and name in a table.

docker volume ls --format "table {{.Driver}}\t{{.Name}}"

The \t is used to create a tab space between the columns. The output will be a table with "DRIVER" and "NAME" as headers.

Finally, let's clean up the volumes we created during this lab. We can remove them using the docker volume rm command.

docker volume rm myvolume another_volume

You should see confirmation that the volumes have been removed.

Summary

In this lab, we learned how to manage Docker volumes using the docker volume ls command. We began by creating sample volumes using docker volume create. Subsequently, we explored how to list all existing volumes on the system using docker volume ls.

Furthermore, we delved into filtering volumes to locate specific ones. We learned to filter volumes by name and driver using the --filter flag with the name and driver keys. The lab also covered filtering volumes based on their dangling status and formatting the output of the docker volume ls command for better readability.