How to use docker system prune command to reclaim disk space

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker system prune command to reclaim disk space by removing unused Docker data. We will start by understanding what docker system prune removes by default, which includes exited containers, dangling images, and dangling networks, but not volumes.

Following this, you will explore how to prune unused Docker data while specifically excluding volumes, and then learn how to remove all unused data, including anonymous volumes. Finally, you will discover how to use filters with docker system prune to gain more granular control over what data is removed.


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/ps("List Running Containers") docker/ImageOperationsGroup -.-> docker/images("List Images") docker/VolumeOperationsGroup -.-> docker/volume("Manage Volumes") docker/SystemManagementGroup -.-> docker/system("Manage Docker") docker/SystemManagementGroup -.-> docker/prune("Remove Unused Docker Objects") subgraph Lab Skills docker/ps -.-> lab-555250{{"How to use docker system prune command to reclaim disk space"}} docker/images -.-> lab-555250{{"How to use docker system prune command to reclaim disk space"}} docker/volume -.-> lab-555250{{"How to use docker system prune command to reclaim disk space"}} docker/system -.-> lab-555250{{"How to use docker system prune command to reclaim disk space"}} docker/prune -.-> lab-555250{{"How to use docker system prune command to reclaim disk space"}} end

Understand what docker system prune removes by default

In this step, we will explore the docker system prune command and understand what types of unused Docker data it removes by default. This command is useful for cleaning up disk space by removing dangling or unused objects.

First, let's create some unused Docker objects. We will run a simple container and then stop it. This will leave behind an exited container and potentially a dangling image if the image is not used by any other container.

Open the terminal in the LabEx environment. The default directory is ~/project.

Run a simple hello-world container:

docker run hello-world

You should see output indicating that the Docker daemon pulled the hello-world image and ran the container. The container will exit immediately after printing its message.

Now, list all containers, including the exited ones:

docker ps -a

You will see the hello-world container listed with a status of "Exited".

Next, let's list the images:

docker images

You should see the hello-world image.

Now, let's run the docker system prune command without any options. This will remove dangling images, dangling containers, and dangling networks. It will not remove volumes by default.

docker system prune -f

We use the -f flag to force the removal without a confirmation prompt.

After running the command, you will see output indicating what has been removed.

Now, let's check the containers again:

docker ps -a

The exited hello-world container should be gone.

Check the images again:

docker images

The hello-world image might still be present if it's not considered "dangling" (e.g., if it was recently pulled). However, if you had other dangling images, they would have been removed.

The key takeaway here is that docker system prune by default cleans up exited containers, dangling images, and dangling networks. It does not remove volumes unless you explicitly tell it to.

Prune unused Docker data without removing volumes

In the previous step, we saw that docker system prune by default does not remove volumes. Volumes are used to persist data generated by and used by Docker containers. Removing volumes unintentionally can lead to data loss.

In this step, we will demonstrate how docker system prune works without removing volumes. We will create a volume, run a container that uses it, stop the container, and then prune the system. We will observe that the volume remains after pruning.

First, let's create a named volume. Named volumes are explicitly created and managed by Docker.

docker volume create myvolume

You should see the name of the created volume as output.

Now, let's run a simple container that uses this volume. We will use the ubuntu image. If you don't have it locally, Docker will pull it.

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

This command runs an ubuntu container in detached mode (-d), names it mycontainer, mounts the myvolume to the /app directory inside the container, and keeps the container running for 60 seconds using the sleep 60 command.

Check that the container is running:

docker ps

You should see mycontainer listed.

Now, stop the container:

docker stop mycontainer

The container will stop, but it will still exist in an exited state.

Verify the container is stopped:

docker ps -a

You should see mycontainer with a status of "Exited".

Now, let's list the volumes:

docker volume ls

You should see myvolume listed.

Now, run docker system prune again. Remember, by default, it doesn't remove volumes.

docker system prune -f

Observe the output. It should indicate that the exited container was removed, but it should not mention removing the volume.

Verify that the container is gone:

docker ps -a

The mycontainer should no longer be listed.

Finally, verify that the volume still exists:

docker volume ls

You should still see myvolume listed. This confirms that docker system prune by default preserves volumes.

Prune all unused Docker data including anonymous volumes

In the previous steps, we learned that docker system prune by default does not remove volumes. However, there are situations where you might want to remove all unused Docker data, including volumes that are no longer associated with any container. This is particularly useful for cleaning up anonymous volumes, which are created when you mount a directory inside a container without explicitly naming a volume.

To remove all unused Docker data, including volumes, you can use the -a flag with docker system prune. The -a flag stands for "all" and includes volumes in the pruning process.

Let's create an anonymous volume by running a container and mounting a directory without specifying a volume name.

docker run -d --name anothercontainer -v /data ubuntu sleep 60

This command runs an ubuntu container named anothercontainer and mounts an anonymous volume to the /data directory inside the container.

Check that the container is running:

docker ps

You should see anothercontainer listed.

Now, stop the container:

docker stop anothercontainer

The container will stop and become an exited container.

Verify the container is stopped:

docker ps -a

You should see anothercontainer with a status of "Exited".

Now, list the volumes:

docker volume ls

You will see the myvolume from the previous step and a new volume with a long, randomly generated name. This is the anonymous volume.

Now, run docker system prune with the -a flag to remove all unused data, including volumes.

docker system prune -a -f

We use -a to include volumes and -f to force the removal without a confirmation prompt.

Observe the output carefully. It should indicate that the exited container was removed, and it should also list the anonymous volume that was removed. The named volume myvolume from the previous step should also be removed because it is no longer used by any container.

Verify that all containers are gone:

docker ps -a

There should be no containers listed.

Finally, verify that all volumes are gone:

docker volume ls

There should be no volumes listed. This confirms that docker system prune -a removes all unused Docker data, including both named and anonymous volumes.

Prune Docker data using filters

In addition to removing all unused data, docker system prune allows you to use filters to selectively remove Docker objects. This gives you more granular control over what gets pruned.

Filters can be applied to different types of objects. For docker system prune, you can filter based on criteria like the age of the objects.

Let's create some more Docker objects to demonstrate filtering. We will run a couple of containers and then stop them.

Run the first container:

docker run -d --name oldcontainer ubuntu sleep 10

This container will run for 10 seconds and then exit.

Run the second container:

docker run -d --name newcontainer ubuntu sleep 20

This container will run for 20 seconds and then exit.

Wait for both containers to exit. You can check their status with docker ps -a.

docker ps -a

You should see both oldcontainer and newcontainer with "Exited" status.

Now, let's use a filter to remove only objects that are older than a certain time. We can use the until filter with a duration. For example, to remove objects older than 5 seconds, you can use until=5s.

Let's try pruning objects older than 15 seconds. This should remove oldcontainer but keep newcontainer (assuming you waited long enough for oldcontainer to exit but not newcontainer).

docker system prune -f --filter "until=15s"

Observe the output. It should indicate that oldcontainer was removed, but newcontainer should remain.

Verify the containers:

docker ps -a

You should see newcontainer listed, but oldcontainer should be gone.

Now, let's prune again, this time removing objects older than 30 seconds. This should remove newcontainer as well.

docker system prune -f --filter "until=30s"

Observe the output. It should indicate that newcontainer was removed.

Verify the containers again:

docker ps -a

There should be no containers listed.

Filters provide a powerful way to customize the pruning process. You can use different filter keys depending on the type of object you are pruning (containers, images, volumes, networks). The until filter is commonly used to clean up objects that have been unused for a specific duration.

Summary

In this lab, we learned how to use the docker system prune command to reclaim disk space by removing unused Docker data. We started by understanding the default behavior of docker system prune, which removes exited containers, dangling images, and dangling networks. We demonstrated this by creating and then removing an exited container.

We then explored how to use docker system prune with options to control what data is removed. We learned how to prune unused data without removing volumes and how to include anonymous volumes in the pruning process. Finally, we touched upon using filters to selectively prune Docker data based on specific criteria. This lab provided practical experience in using docker system prune to effectively manage disk space in a Docker environment.