Do you have time in a project where you have been working for quite a long time, and you create and tag dozens of Docker images daily. But, after some time, you generally forget how many docker images you have built and tagged. Thanks God there is a way to get out of this mess. Let’s talk about how to remove old and unused Docker images.
Docker’s conservative approach to remove old and unused Docker Images
Docker seems to have a very conservative approach to deleting redundant objects. They are often referred to as “garbage collections” such as images, containers, volumes, and networks. Usually, Docker does not remove these objects if users do not have explicit demand.
This conservative approach has its pros and cons. On the positive side, when you somehow look for an ancient object in the project, it probably stays where it is. But this might return to you as a shortage in disk space. Therefore, Docker 1.13: PR 26108 and commit 86de7c0 to introduce a few new commands to help visualize how much space the docker daemon data is taking on disk. The commands allow us to clean up “unneeded” excess efficiently.
docker image prune command enables you to remove unused images. By default,
docker image prune only cleans up dangling images, which means not tagged and not referenced by any container. To remove dangling images:
docker image prune
To remove all images which are not used by existing containers, use the
-a flag (for removing dangling and unused images).
—Warning!!! This will remove all images without at least one container associated with them, SO be careful before using
docker image prune -a
You will get a warning similar to the above also from Docker, and it will ask you whether you want to continue. So to bypass the prompt, use the
docker image prune -a --force
You can limit which images are pruned using filtering expressions with the
--filter flag. For example, to only consider images created more than 24 hours ago:
docker image prune -a --filter "until=24h"
Another example, to remove all images (of course not used by existing containers) before a specific date and time:
docker image prune -a --force --filter "until=2017-01-04T00:00:00"
While using filtering expressions
"label=<key>=<value>" with the
--filter flag, only remove images with the specified labels. The following example removes images with the label
docker image prune --filter="label=deprecated"
TIPSSS!!! If you are using positive filtering (testing for the existence of a label or that a label has a specific value), you can use
docker image ls with the same filtering syntax to see which images match your filter. For example, to list the images in the above before deleting them:
docker image ls --filter="label=deprecated"
Finally.. and BONUS
Today we learned how to deal with our obsolete images. Now you know how to remove old and unused Docker images. Similarly, you can remove old and unused Docker containers, volumes, and networks. Even it is possible to clean up all of the objects with a single command. Find this magic command below as a BONUS. However, for details, I strongly recommend you browsing through the Docker documentation page.
docker system prune --volumes
Which commands do you use the most to remove old and unused Docker objects? Please share us in the comments.