Cleaning local docker cache

Context #

Recently, my laptop was running out of space. I had the impression that docker would be the culprit and went to check its disk space usage, using the following command:

docker system df

It turns out that Docker was consuming close to 40% of the disk space available!

Solution #

Let's go through the different options to free hard disk space.

Removing unused containers #

Docker containers have a status field indicating where they are at in their lifecycle. According to the docs, status can be one of created, restarting, running, removing, paused, exited, or dead.

First, we need to get the IDs of the containers with status exited or dead as follows:

docker ps --filter status=exited --filter status=dead -q

Then, we can reuse the above command to delete these containers with the following command:

docker rm $(docker ps --filter=status=exited --filter=status=dead -q)

A one-liner alternative to remove all stopped containers is:

docker container prune

Removing all containers #

First, we need to stop all running containers. We can get the IDs of the running containers as follows:

docker ps -q

Then, we can stop all the containers with:

docker stop $(docker ps -q)

You can replace docker stop with docker kill in the above command to forcibly stop the containers.

Finally, we can delete all containers:

docker rm $(docker ps -a -q)

Removing dangling images #

Dangling images, as mentioned by the documentation, are final images (i.e, not intermediary build layers) that no longer have an associated tag with them.

We can get the image ID for such images as follows:

docker images --filter dangling=true -q

Then, we can delete those images with the following command:

docker rmi $(docker images --filter dangling=true -q)

A one-liner alternative to remove all dangling images is:

docker image prune

Removing all images #

Docker doesn't allow to remove images that have an associated container, so to really delete all images, it is necessary first to remove all containers.

Similarly to the previous section, we need the IDs of all the images, which we can get using:

docker images -a -q

Then, we can combine it with docker rmi:

docker rmi $(docker images -a -q)

A one-liner alternative to remove all images is:

docker image prune -a

Removing volumes #

Volumes also take space in the host machine. They are never deleted automatically since they may contain data that can be reused by different containers or directly from the host.

Then, to remove all docker volumes use:

docker volume prune

Removing networks #

Although docker networks don't consume too much disk space, they create iptables rules, network devices and routing table entries. To prune these objects, you can run:

docker network prune

Removing everything #

Instead of manually pruning different types of resources, you may be interested on wiping out everything from your local cache. For that we can leverage the docker system prune command as follows:

To remove containers, images and networks use:

docker system prune

To remove containers, images, networks and volumes, use

docker system prune --volumes

References #

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.