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

Viewed 99008

In the docker documentation of docker image prune it is possible to use the -a flag to

Remove all unused images, not just dangling ones

and later

Remove all dangling images. If -a is specified, will also remove all images not referenced by any container.

Can someone explain to me what dangling images are and what's the difference between dangling and unused images?

8 Answers

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

An unused image is an image that has not been assigned or used in a container.

To list dangling images:

docker images -f dangling=true

Dangling images are untagged images. The following command gives a list of dangling images.

docker images --filter "dangling=true"

docker image prune deletes all dangling images.

Unused images are images that have tags but currently not being used as a container. You may or may not need it in future.

docker image prune -a delete all dangling as well as unused images.

You generally don't want to remove all unused images until some time. Hence it is better to remove with a filter.

docker image prune -a -f --filter "until=6h"

In the images screenshot, "none" name are dangling images. 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 "" on its name when you run docker images.

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.

I saw useful commands (aliases) for removing dangling images, courtesy of andyneff here: https://forums.docker.com/t/how-to-delete-cache/5753:

alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)' 
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)' 

The first one cleans all dangling images. This is useful for removing intermediate images left over from multiple builds. The second one is for removing stopped containers. These are aliases I use for routine maintenance

If you want to remove ALL of your cache, you first have to make sure all containers are stopped and removed, since you cannot remove an image in use by a container. So something similar

docker kill $(docker ps -q) docker_clean_ps docker rmi $(docker images
-a -q)

This would kill and remove all images in your cache.

docker images -aq -f dangling=true | xargs docker rmi -f 
Related