Using the docker command line, how can I get a list of only containers that use an image…
- User
Using the docker command line, how can I get a list of only containers that use an image that contains 'buildkit' as part of their tag?
- Assistant
Here are a few ways to “filter” your running containers down to only those whose image‐tag contains the substring
buildkit. Pick the one you like best:-
Simple grep on the CLI
# show CONTAINER ID and IMAGE, then grep for "buildkit" in the tag docker ps --format "{{.ID}}\t{{.Image}}" \ | grep buildkitIf you want only the IDs:
docker ps --format "{{.ID}}\t{{.Image}}" \ | awk -F'\t' '/buildkit/ {print $1}' -
Using jq on the JSON output
docker ps --format '{{json .}}' \ | jq -r 'select(.Image|test("buildkit")) | .ID + " " + .Image' -
(LSH: non‐wildcard but “official”)
--filter=ancestor=
Docker’sancestor=filter only matches exact repository[:tag] or prefix, so you’d have to enumerate each matching image first:# 1) find all image names/tags containing "buildkit" images=( $(docker images --format '{{.Repository}}:{{.Tag}}' \ | grep buildkit) ) # 2) build up multiple --filter flags filters=() for img in "${images[@]}"; do filters+=(--filter "ancestor=$img") done # 3) list containers matching ANY of those filters docker ps "${filters[@]}"
In practice most people go with option 1 or 2 (shell‐piping or jq) since it’s the easiest.
-
- User
I'd like to do this in a single line. The
--filters "ancestor=*buildkit*"doesn't work because ancestor doesn't seem to support wildcards. - Assistant
Here are two common “one-liners”:
- Grep the
IMAGEcolumn:
docker ps --format '{{.ID}}\t{{.Image}}' \ | grep buildkit– If you only want the container IDs:
docker ps --format '{{.ID}}\t{{.Image}}' \ | awk '$2 ~ /buildkit/ {print $1}'- Dynamically build
--filter ancestor=flags:
docker ps $( docker images --format '{{.Repository}}:{{.Tag}}' \ | grep buildkit \ | sed 's/^/--filter ancestor=/' \ | xargs )This will expand to something like:
docker ps --filter ancestor=myrepo:buildkit --filter ancestor=otherrepo:buildkit-v2 ...Pick whichever suits your taste!
- Grep the
- User
I really just want the ID of the container. Alternately, a one-liner to remove such containers would suffice.
- Assistant
Here’s a couple of one-liners that will give you exactly the container IDs (or remove them) for any running/stopped containers whose image‐tag contains “buildkit”:
- Just list the IDs
docker ps -a --format '{{.ID}} {{.Image}}' \ | awk '/buildkit/ {print $1}'- Yank those IDs straight into
docker rm
docker ps -a --format '{{.ID}} {{.Image}}' \ | awk '/buildkit/ {print $1}' \ | xargs docker rmIf you want to force‐stop & remove in one go:
docker ps -a --format '{{.ID}} {{.Image}}' \ | awk '/buildkit/ {print $1}' \ | xargs docker rm -fExplanation:
--format '{{.ID}} {{.Image}}'prints “<CONTAINER_ID> IMAGE:TAG”awk '/buildkit/ {print $1}'filters on the tag and spits out just the IDxargs docker rmremoves them (add-fto force-remove running ones)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.