Cleaning up Docker

Cleaning up Docker

In this post, we will see different ways to remove unused resources allocated by Docker.

I believe that Docker is a game-changer technology, it's a very efficient way to ship applications and promotes better compatibility among different environments where applications run (e.g. development, staging, production).

Docker tends to store a lot of resources to improve its performance on the host's machine. However, these resources are not really in use by the most recent applications running in the host. How can we deal with it?

Let's examine what Docker is using, we can do that by running this command:

docker system df

This command displays information regarding the amount of disk space used by the docker daemon. You will see an output like that:

Result of "docker system df"

A first approach to eliminate unused resources can be to use the command:

docker system prune

It will remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

With this simple command, in this case, we were able to free up almost 2GB of the host's disk.

Another approach can be to focus on reducing the space used by volumes. We can do that using the following command:

docker volume prune


You could also check other most "aggressive" approaches that were not the focus of this post, like removing docker overlays, inspecting images and removing unused ones, using images smaller (such as images based on Alpine Linux) and so on.