docker how to list all unused images without pruning

Viewed 492

enter image description here

I've found that docker system df shows the large RECLAIMABLE space for me.
In order to save the space, I know that docker image prune -a will remove all unused images.
However, I'd like to know the list before pruning for the safety.
Is there any way to list all images those are currently not being used by any container? (The images that will be deleted by docker image prune -a)
I have searched for it but there are only pruning methods, no listing methods.

2 Answers

So to fetch all the unused docker images without actually pruning them, you could do the following

  1. Fetch all the images belonging to the running containers(which are not stopped or exited)

  2. Fetch all the images on the machine

  3. Then filter the images in step 1 from step 2

Below are the shell commands

runningImages=$(docker ps --format {{.Image}})
docker images --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep -v "$runningImages"

you can use this command to show all of images:

docker images -a

You can find unused images using the command:

docker images -f dangling=true

and just a list of their IDs:

docker images -q -f dangling=true

In case you want to delete them:

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