logo.svg

Deleting old Docker images

author-avatar
Lucas Felicio Adriano

Using Docker for local development is amazing: you don't need to install any funky dependencies on your PC, no need to run a bunch of services on the background, and, as the cherry on top, when you stop working on a project all you got to do to remove the existing infrastructure is to run docker-compose down or docker rm -f container_name_or_id.

But have you already noticed that as time goes on and you start download more and more images your computer's free space start to disappear? That happens because Docker caches all images you have ever downloaded or built, even though you might not be using it used anymore. That happens to speed-up things in case you ever need to build the same image again, considering it's already stored and cached on your PC.

Caching is a good thing, but we should remove these stale layers (this is how Docker calls parts of an image cached) from time to time to get some of that precious SSD space back. What's the best way to do it then?

Checking HD space being used by Docker


One way to check the space being currently used by Docker is by issuing the following command:


docker system df


This command will show you the total space being used by images, containers and volumes you have created.



Deleting old images and containers


There are a few different ways to delete this data to clear space on your SSD, however I'll show here two different methods to do it: one that's more effective, which ends up deleting more data, and another one that is a little faster, but ends up deleting less data on the process. Which one you should use is completely up to you and is going to depend on how many space do you need to reclaim :)

The faster way


To remove all stopped containers, networks not being used anymore and images and cache layers untagged you can issue the following command:


docker system prune


Before actually deleting your data Docker will ask you if you want to proceed with the removal and warn you about everything being deleted:



The more efficient way


The command docker system prune has an additional parameter named --all or -a, which when set will make Docker to also delete all unused images and cache layers (even the ones with tags).

This variation is much more effective, resulting in much more free space after its execution, however you should beware that it might end up removing more things that you were originally planing. Use it with caution.



Bonus: Removing old images and containers automatically


To keep running these commands manually from time to time may sound a little boring (and easy to forget), and it really is. You never know exactly how often you should delete this old data unless you run the df command before. With that in mind I came up with a solution rather simple to automate this process: adding the docker removal command to crontab.


0 */2 * * * docker system prune -f


This expression is telling crontab to run the docker system prune command once every two hours (automatically). The -f or --force parameter is used to skip the confirmation shown before actually removing the data.


bash
docker