Skip to content
The Chat Archive
o4-mini-2025-04-16· OpenAI LMArena Human Preference 140K

Using the docker command line, how can I get a list of only containers that use an image…

  1. 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?

  2. 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:

    1. Simple grep on the CLI

      # show CONTAINER ID and IMAGE, then grep for "buildkit" in the tag
      docker ps --format "{{.ID}}\t{{.Image}}" \
        | grep buildkit
      

      If you want only the IDs:

      docker ps --format "{{.ID}}\t{{.Image}}" \
        | awk -F'\t' '/buildkit/ {print $1}'
      
    2. Using jq on the JSON output

      docker ps --format '{{json .}}' \
        | jq -r 'select(.Image|test("buildkit")) | .ID + "  " + .Image'
      
    3. (LSH: non‐wildcard but “official”) --filter=ancestor=
      Docker’s ancestor= 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.

  3. 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.

  4. Assistant

    Here are two common “one-liners”:

    1. Grep the IMAGE column:
    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}'
    
    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!

  5. User

    I really just want the ID of the container. Alternately, a one-liner to remove such containers would suffice.

  6. 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”:

    1. Just list the IDs
    docker ps -a --format '{{.ID}} {{.Image}}' \
      | awk '/buildkit/ {print $1}'
    
    1. Yank those IDs straight into docker rm
    docker ps -a --format '{{.ID}} {{.Image}}' \
      | awk '/buildkit/ {print $1}' \
      | xargs docker rm
    

    If you want to force‐stop & remove in one go:

    docker ps -a --format '{{.ID}} {{.Image}}' \
      | awk '/buildkit/ {print $1}' \
      | xargs docker rm -f
    

    Explanation:

    • --format '{{.ID}} {{.Image}}' prints “<CONTAINER_ID> IMAGE:TAG”
    • awk '/buildkit/ {print $1}' filters on the tag and spits out just the ID
    • xargs docker rm removes them (add -f to force-remove running ones)

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.