How to remove all the containers according with a removed image's id?

Hello

For experimental purposes was executed the following command:

sudo docker rmi -f ubuntu:20.04

which prints:

Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:0b897358ff6624825fb50d20ffb605ab0eaea77ced0adb8c6a4b756513dec6fc
Deleted: sha256:5f5250218d28ad6612bf653eced407165dd6475a4daf9210b299fed991e172e9

Now if is executed the sudo docker ps -a command is displayed

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS     NAMES
...
ed096064373f   5f5250218d28   "cat /etc/os-release"    19 hours ago   Exited (0) 19 hours ago             ubuntu-20.18-d-015
3bc4c52f0299   5f5250218d28   "cat /etc/os-release"    19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-015
8cd0502ff777   5f5250218d28   "ls -als ../etc"         19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-014
d3ce9a3fbd47   5f5250218d28   "ls -als /"              19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-013
c8889c44f3a9   5f5250218d28   "ls -als ."              19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-012
1c6ff666c7ff   5f5250218d28   "ls -als /home/manueā€¦"   19 hours ago   Exited (2) 19 hours ago             ubuntu-20.04-d-011
b5da7429b8b8   5f5250218d28   "ls -als /etc"           19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-010
f169712b9718   5f5250218d28   "ls -als /etc"           19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-009
5ea2001f47e2   5f5250218d28   "ls -als /home"          19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-008
cde6c940efb4   5f5250218d28   "ls -als /home/manueā€¦"   19 hours ago   Exited (2) 19 hours ago             ubuntu-20.04-d-007
f75c3f7932cd   5f5250218d28   "ls -als ."              19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-006
7d7154a21668   5f5250218d28   "cat /etc/os-release"    19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-005
a75b22a43af7   5f5250218d28   "echo hello"             19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-004
2fcfb4dea168   5f5250218d28   "cat /home/manueljorā€¦"   19 hours ago   Exited (1) 19 hours ago             ubuntu-20.04-d-003
8c9def0115c8   5f5250218d28   "date"                   19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-002
f2a7ad7904a8   5f5250218d28   "ls /etc"                19 hours ago   Exited (0) 19 hours ago             ubuntu-20.04-d-001
...

Observe the IMAGE header value. It shows the 5f5250218d28 value where it is the first 12 characters of sha256:5f5250218d28ad6612bf653eced407165dd6475a4daf9210b299fed991e172e9

Now, I want to remove all these containers based on the Imagesā€™s id shown in the IMAGE header.

Thus I did do a research and I found:

Where is indicated the following command:

  • docker rm $(docker ps -a -q --filter "ancestor=ubuntu")

Now, just playing with the command substitution part and by applying the following variations as follows:

docker ps -aq --filter "ancestor=5f5250218d28"
docker ps -aq --filter "ancestor=sha256:5f5250218d28"
docker ps -aq --filter "ancestor=5f5250218d28ad6612bf653eced407165dd6475a4daf9210b299fed991e172e9"
docker ps -aq --filter "ancestor=sha256:5f5250218d28ad6612bf653eced407165dd6475a4daf9210b299fed991e172e9"

All of them returns nothing

Just in case if is executed

sudo docker ps -aq --filter "name=ubuntu-20.04-d-"

It prints the expected set of containerā€™s id but not always the containers would have the same pattern name

Question

  • How to remove all the containers according with the imageā€™s id?

So I need resolve first the command substitution part about the ps -aq --filter command

Iā€™ve read the official documentation about:

And it seems is not possible. But I am assuming would exists a trick/smart approach

Thank You

Since your containers were already stopped, the forced delete deleted the image metadata completely, not just the tag. It seems in this case the ancestor filter will not work as the ancestor doesnā€™t exist anymore. You will need to rely on the existing metadata of the container completely. Replace your filter command in the subshell with this one:

docker ps -a --format '{{ if eq .Image "5f5250218d28" }}{{ .ID }}{{ end }}' | grep "\S"

Or if you just want to remove all the stopped containers:

docker container prune
2 Likes

Huge thanks

The first command works.

Thanks again

Just in case, if you want, you can share the same answer at:

Done. I actually added more explanation and an alternative solution if anyone is interested. In short, the alternative command is:

docker ps -a --format json \
  | jq -r '. | select(.Image == "3c2285410b59") | .ID'

It can be useful if you are more familiar with jq than Go templates

1 Like

Huge thanks again for your polite support