How to use docker volume prune command to remove unused volumes

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage Docker volumes, focusing on the docker volume prune command. We will begin by creating and inspecting various types of volumes, including named volumes and volumes with labels, to understand their properties and how Docker manages them.

Following this, we will demonstrate how volumes are used with containers by creating and then removing a container that utilizes a volume. This will highlight the persistence of data stored in volumes. Finally, the lab will guide you through using the docker volume prune command to remove unused volumes, covering the removal of anonymous volumes, all unused volumes (including named volumes), and using label filters to selectively prune volumes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") docker/SystemManagementGroup -.-> docker/prune("Remove Unused Docker Objects") subgraph Lab Skills docker/run -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/ls -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/rm -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/inspect -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/pull -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/volume -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} docker/prune -.-> lab-555261{{"How to use docker volume prune command to remove unused volumes"}} end

Create and inspect some volumes

In this step, we will learn how to create and inspect 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.

First, let's create a named volume using the docker volume create command. We will name this volume myvolume.

docker volume create myvolume

You should see the name of the volume printed to the console, confirming its creation.

Now, let's inspect the volume we just created using the docker volume inspect command. This command provides detailed information about the volume, such as its name, driver, and mountpoint on the host machine.

docker volume inspect myvolume

The output will be a JSON array containing information about the volume. Look for the "Mountpoint" field, which shows the location on the host where the volume's data is stored. Docker manages this location, and you should generally avoid modifying files directly in this directory.

Next, let's create another volume, this time with a label. Labels are useful for organizing and filtering Docker objects. We will create a volume named labeled_volume and add a label app=web.

docker volume create --label app=web labeled_volume

Again, the name of the volume will be printed upon successful creation.

Finally, let's inspect the labeled_volume to see the label we added.

docker volume inspect labeled_volume

In the JSON output, you should find a "Labels" field containing the app=web label.

Create and remove a container using a volume

In this step, we will create a Docker container and attach the myvolume we created in the previous step to it. This will demonstrate how data can persist even after the container is removed.

First, let's pull the ubuntu image, which we will use for our container.

docker pull ubuntu

This command downloads the ubuntu image from Docker Hub.

Now, we will run a container based on the ubuntu image and mount myvolume to the /app directory inside the container. We will also run a command that writes some data to a file within the mounted volume.

docker run -d --name mycontainer -v myvolume:/app ubuntu /bin/bash -c "echo 'Hello from the container!' > /app/data.txt; sleep 30"

Let's break down this command:

  • docker run: This command is used to run a new container.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • --name mycontainer: This assigns the name mycontainer to the container, making it easier to reference.
  • -v myvolume:/app: This is the crucial part for using the volume. It mounts the named volume myvolume to the /app directory inside the container.
  • ubuntu: This specifies the image to use for the container.
  • /bin/bash -c "echo 'Hello from the container!' > /app/data.txt; sleep 30": This is the command executed inside the container. It uses bash to run two commands: first, it writes the string "Hello from the container!" to a file named data.txt inside the /app directory (which is the mounted volume), and then it makes the container sleep for 30 seconds to keep it running for a short period.

You can check if the container is running using the docker ps command.

docker ps

You should see mycontainer listed in the output with a status of "Up".

Now, let's remove the container. Even though we remove the container, the data written to the volume should persist.

docker rm -f mycontainer

The -f flag forces the removal of the container, even if it's still running.

To verify that the data persists in the volume, we can run a new container and mount the same volume, then check the content of the file.

docker run --rm -v myvolume:/app ubuntu cat /app/data.txt

Let's break down this command:

  • docker run: Runs a new container.
  • --rm: This flag automatically removes the container when it exits.
  • -v myvolume:/app: Mounts the myvolume to /app in this new container.
  • ubuntu: Uses the ubuntu image.
  • cat /app/data.txt: This is the command executed inside the container. It uses cat to display the content of the data.txt file located in the /app directory.

You should see "Hello from the container!" printed to the console, confirming that the data persisted in the volume even after the original container was removed.

Prune unused anonymous volumes

In this step, we will learn how to remove unused anonymous volumes. Anonymous volumes are created when you don't explicitly name a volume when mounting it to a container. They are typically removed when the container that created them is removed, but sometimes they can persist. Pruning helps clean up these unused volumes.

First, let's create a container with an anonymous volume. We will run a simple container that exits immediately.

docker run --rm -v /data ubuntu ls /data

Let's break down this command:

  • docker run: Runs a new container.
  • --rm: This flag automatically removes the container when it exits.
  • -v /data: This creates an anonymous volume and mounts it to the /data directory inside the container. Since we didn't provide a name before the colon, Docker generates a random name for the volume.
  • ubuntu: Uses the ubuntu image.
  • ls /data: The command executed inside the container, which lists the contents of the /data directory.

Even though the container is removed due to the --rm flag, the anonymous volume might still exist if it wasn't explicitly cleaned up by Docker.

To see all volumes, including anonymous ones, you can use the docker volume ls command.

docker volume ls

You should see myvolume and labeled_volume from the previous steps, and potentially one or more volumes with long, random-looking names. These are the anonymous volumes.

Now, let's prune the unused anonymous volumes using the docker volume prune command. By default, this command only removes anonymous volumes that are not currently used by any container.

docker volume prune

Docker will ask for confirmation before proceeding. Type y and press Enter.

The output will show you which volumes were removed. You should see the anonymous volume(s) that were not in use being removed.

Let's list the volumes again to confirm that the anonymous volumes have been removed.

docker volume ls

You should now only see the named volumes (myvolume and labeled_volume) in the list.

Prune all unused volumes including named volumes

In the previous step, we pruned unused anonymous volumes. In this step, we will learn how to prune all unused volumes, including named volumes that are not currently attached to any container.

First, let's list the existing volumes to see what we have.

docker volume ls

You should see myvolume and labeled_volume listed. These are named volumes and were not removed by the default docker volume prune command because they are named.

To prune all unused volumes, including named ones, we need to use the --all or -a flag with the docker volume prune command.

docker volume prune --all

Docker will again ask for confirmation. Type y and press Enter.

The output will show you which volumes are being removed. Since myvolume and labeled_volume are not currently used by any container, they will be removed.

Let's list the volumes again to confirm that all unused volumes have been removed.

docker volume ls

You should now see an empty list, indicating that all unused volumes, both anonymous and named, have been removed.

Prune volumes using label filter

In this final step, we will learn how to prune volumes based on labels. This is a powerful way to selectively remove volumes that match specific criteria, without affecting other volumes.

First, let's create a couple of new volumes with different labels so we have something to filter.

docker volume create --label env=dev dev_volume
docker volume create --label env=prod prod_volume
docker volume create --label type=data data_volume

We have created three new volumes: dev_volume with label env=dev, prod_volume with label env=prod, and data_volume with label type=data.

Let's list the volumes to see the newly created ones.

docker volume ls

You should see dev_volume, prod_volume, and data_volume in the list.

Now, let's prune only the volumes that have the label env=dev. We can use the --filter flag with the label key.

docker volume prune --filter label=env=dev

Docker will ask for confirmation. Type y and press Enter.

The output will show that dev_volume was removed.

Let's list the volumes again to confirm that only dev_volume was removed.

docker volume ls

You should now see prod_volume and data_volume remaining.

We can also filter by labels that are not present. For example, let's prune volumes that do not have the label type=data.

docker volume prune --filter label!=type=data

Docker will ask for confirmation. Type y and press Enter.

The output will show that prod_volume was removed.

Let's list the volumes one last time to see what's left.

docker volume ls

You should now only see data_volume remaining.

This demonstrates how you can use label filters to selectively prune volumes based on your needs.

Summary

In this lab, we learned how to manage Docker volumes, which are the preferred method for persisting container data. We began by creating and inspecting both named volumes and volumes with labels using docker volume create and docker volume inspect. This allowed us to understand how volumes are created and view their details, including their mountpoints and associated labels.

Subsequently, we demonstrated the persistence of data by creating a container that utilized a named volume and then removing the container. This highlighted that the volume and its data remained intact after the container's removal. Finally, we explored the docker volume prune command, learning how to remove unused anonymous volumes by default, how to remove all unused volumes (including named volumes) using the --all flag, and how to selectively prune volumes based on labels using the --filter option. These steps provided practical experience in cleaning up unused volumes to free up disk space.