How to remove unused Docker resources

alejandroakbal

Alejandro Akbal

Posted on December 16, 2020

How to remove unused Docker resources

Introduction

Chances are that you've been running Docker for some time and found out that your system's storage is almost full.

This is completely normal as Docker bundles all the needed dependencies with each container and doesn't remove anything if you don't explicitly tell it to do so.

So lets learn how to prune unused and unnecessary images, containers, volumes and networks!

This tutorial will help you liberate space on your system without breaking anything in the process.


Before we start

We will be using the Docker CLI, so I expect you to be a bit familiar with it.

Otherwise just use docker --help on the terminal and toy with it a little.

Requisites

  • A little bit of Docker knowledge

Images

Remove all images that are not tagged or referenced by any container

docker image prune
Enter fullscreen mode Exit fullscreen mode

Containers

Remove all stopped containers

docker container prune
Enter fullscreen mode Exit fullscreen mode

Volumes

Remove all volumes not used by at least one container

docker volume prune
Enter fullscreen mode Exit fullscreen mode

Networks

Remove all networks not used by at least one container

docker network prune
Enter fullscreen mode Exit fullscreen mode

Everything

To finalize, lets remove everything --but volumes-- with a single command.

docker system prune
Enter fullscreen mode Exit fullscreen mode

If you want to remove volumes too, just append --volumes at the end.

docker system prune --volumes
Enter fullscreen mode Exit fullscreen mode

And voila, that removed every single resource that was unnecessary on your system!


Troubleshooting

You might find that some images can't be removed because they are used. In this case you want to remove the resource that is using it, most likely a container.


End

What's next?

If you want to read more, please check out the official Docker guide to pruning.

Self promotion

If you have found this useful then you should follow me, I will be posting more interesting content! 🥰

Or support me financially. 💸

Conclusion

Today you have learned how to free up space on your system by removing Docker's unused images, containers, volumes and networks.

Let me know how much space you have recovered in the comments!

💖 💪 🙅 🚩
alejandroakbal
Alejandro Akbal

Posted on December 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to remove unused Docker resources
docker How to remove unused Docker resources

December 16, 2020