1

Using docker image prune we can remove dangling or unused images.

So, I'm wondering if there is a way to list all images used by at least one container, also listing - for each image - the containers that are using them.

Is there some out-of-the-box command in Docker or should I write a script to achieve this?

2 Answers 2

2

You can filter the output of docker ps:

docker ps -a | awk '{print "Image "$2" used by container "$1"" }' 

this will print the name of the base image and the name of the container for all containers on your machine.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a quick edit. Should be docker ps -a | tail -n +2 | awk '{print "Image "$2" used by container "$1"" }'
You can also do docker ps -a --format "{{.Image}} {{.Names}} {{.ID}}" | awk '{print "Image "$2" used by container "$1"" }'
1

Elaborating the answer suggested by Paolo, I created this script which matches pretty fine what I had in my mind:

docker ps -a --format "{{.Image}} {{.Names}} {{.ID}}" | sort | awk '{print "Image "$1" used by container "$2" ("$3")" }' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.