How to get the list of dependent child images in Docker?

后端 未结 12 812
耶瑟儿~
耶瑟儿~ 2020-12-12 11:22

I\'m trying to remove an image and I get:

# docker rmi f50f9524513f  
Failed to remove image (f50f9524513f): Error response from daemon: conflict: unable to          


        
相关标签:
12条回答
  • 2020-12-12 12:17

    Install dockviz and follow the branches from the image id in the tree view:

    go get github.com/justone/dockviz
    $(go env GOPATH)/bin/dockviz images --tree -l
    
    0 讨论(0)
  • 2020-12-12 12:20

    Based on slushy and Michael Hoffman answers, if you don't have a ton of images you can use this shell function:

    docker_image_desc() {
      for image in $(docker images --quiet --filter "since=${1}"); do
        if [ $(docker history --quiet ${image} | grep ${1}) ]; then
          docker_image_desc "${image}"
        fi
      done
      echo "${1}"
    }
    

    and then call it using

    docker_image_desc <image-id> | awk '!x[$0]++'
    
    0 讨论(0)
  • 2020-12-12 12:21

    Here's a simple way to get a list of child images that are dependent on a parent image:

    image_id=123456789012
    
    docker images -a -q --filter since=$image_id |
    xargs docker inspect --format='{{.Id}} {{.Parent}}' |
    grep $image_id
    

    That will generate a list of child/parent images IDs, for example (truncated for brevity):

    sha256:abcdefghijkl sha256:123456789012
    

    The left side is the child image ID, the right side is parent image ID that we are trying to delete, but it is saying "image has dependent child images". This tells us that abcdefghijkl is the child that is dependent on 123456789012. So we need to first docker rmi abcdefghijkl, then you can docker rmi 123456789012.

    Now, there may be a chain of dependent child images, so you may have to keep repeating to find the last child.

    0 讨论(0)
  • 2020-12-12 12:22

    If you don't have a huge number of images, there's always the brute-force approach:

    for i in $(docker images -q)
    do
        docker history $i | grep -q f50f9524513f && echo $i
    done | sort -u
    
    0 讨论(0)
  • 2020-12-12 12:24

    I cooked up this to recursively find children, their repo tags, and print what they're doing:

    docker_img_tree() {
        for i in $(docker image ls -qa) ; do
            [ -f "/tmp/dii-$i"  ] || ( docker image inspect $i > /tmp/dii-$i)
            if grep -qiE 'Parent.*'$1 /tmp/dii-$i ; then
                echo "$2============= $i (par=$1)"
                awk '/(Cmd|Repo).*\[/,/\]/' /tmp/dii-$i | sed "s/^/$2/"
                docker_img_tree $i $2===
            fi
        done
    }
    

    Don't forget to delete /tmp/dii-* if your host isn't secure.

    0 讨论(0)
  • 2020-12-12 12:25

    How about:

    ID=$(docker inspect --format="{{.Id}}" "$1")
    IMAGES=$(docker inspect --format="{{if eq \"$ID\" .Config.Image}}{{.Id}}{{end}}" $(docker images --filter since="$ID" -q))
    echo $(printf "%s\n" "${IMAGES[@]}" | sort -u)
    

    It'll print the child image id's, with the sha256: prefix.
    I also had the following, which appends the names:

    IMAGES=$(docker inspect --format="{{if eq \"$ID\" .Config.Image}}{{.Id}}{{.RepoTags}}{{end}}" $(docker images --filter since="$ID" -q))
    

    • ID= Gets the full id of the image
    • IMAGES= Gets all child images that have this image listed as an Image
    • echo... Removes duplicates and echos the results
    0 讨论(0)
提交回复
热议问题