What is a dangling image and what is an unused image?

Safest and Easiest way to cleanup Dangling Images

docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi

Docker images consist of multiple layers. Dangling images, are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space.

Note: I recommend not to use prune in production, because docker system prune -a will remove all the images which are not referenced by the container, by which we can't roll back to the previous release.

To list dangling images by adding the filter flag, -f with a value of dangling=true to the docker images.

List Dangling images

docker images -f dangling=true

Remove Dangling Images

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

OR

docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi

When we run any cron jobs to delete tha dangling stuff use the above to make sure the job runs successfully. Like in Jenkins if we run a free style job with beloow commad job will never fail even if no dangling stuff exists in the machine.

This is the safest and easiest way to cleanup dangling images and get back our disk space back for use.


An unused image means that it has not been assigned or used in a container. For example, when running docker ps -a - it will list all of your exited and currently running containers. Any images shown being used inside any of containers are a "used image".

On the other hand, a dangling image just means that you've created the new build of the image, but it wasn't given a new name. So the old images you have becomes the "dangling image". Those old image are the ones that are untagged and displays "<none>" on its name when you run docker images.

When running docker system prune -a, it will remove both unused and dangling images. Therefore any images being used in a container, whether they have been exited or currently running, will NOT be affected.

Tags:

Docker