How to get the list of dependent child images in D

2019-01-13 03:48发布

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 delete f50f9524513f (cannot be forced) - image has dependent child images

This is the docker version:

# docker version
Client:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 21:49:11 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Thu Mar 10 21:49:11 2016
 OS/Arch:      linux/amd64

but there is no extra information:

# docker images --format="raw" | grep f50f9524513f -C3

repository: debian
tag: 8
image_id: f50f9524513f
created_at: 2016-03-01 18:51:14 +0000 UTC
virtual_size: 125.1 MB

repository: debian
tag: jessie
image_id: f50f9524513f
created_at: 2016-03-01 18:51:14 +0000 UTC
virtual_size: 125.1 MB

How can I get the dependent child images it claims to have?

there are no running nor stopped containers with that image id.

8条回答
看我几分像从前
2楼-- · 2019-01-13 04:26

Short answer: Here is a python3 script that lists dependent docker images.

Long answer: You can see the image id and parent id for all image created after the image in question with the following:

docker inspect --format='{{.Id}} {{.Parent}}' \
    $(docker images --filter since=f50f9524513f --quiet)

You should be able to look for images with parent id starting with f50f9524513f, then look for child images of those, etc.. But .Parent isn’t what you think., so in most cases you would need to specify docker images --all above to make that work, then you will get image ids for all intermediate layers as well.

Here's a more limited python3 script to parse the docker output and do the searching to generate the list of images:

#!/usr/bin/python3
import sys

def desc(image_ids, links):
    if links:
        link, *tail = links
        if len(link) > 1:
            image_id, parent_id = link
            checkid = lambda i: parent_id.startswith(i)
            if any(map(checkid, image_ids)):
                return desc(image_ids | {image_id}, tail)
        return desc(image_ids, tail)
    return image_ids


def gen_links(lines):
    parseid = lambda s: s.replace('sha256:', '')
    for line in reversed(list(lines)):
        yield list(map(parseid, line.split()))


if __name__ == '__main__':
    image_ids = {sys.argv[1]}
    links = gen_links(sys.stdin.readlines())
    trunc = lambda s: s[:12]
    print('\n'.join(map(trunc, desc(image_ids, links))))

If you save this as desc.py you could invoke it as follows:

docker images \
    | fgrep -f <(docker inspect --format='{{.Id}} {{.Parent}}' \
        $(docker images --all --quiet) \
        | python3 desc.py f50f9524513f )

Or just use the gist above, which does the same thing.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-13 04:26

You can delete Docker images irrespective of parent and child relation through the below directory of Docker

/var/lib/docker/image/devicemapper/imagedb/content/sha256

In this directory you can find Docker images, so you can delete what you want.

查看更多
家丑人穷心不美
4楼-- · 2019-01-13 04:26

I was also facing the same issue. Fallowed steps below to resolve the issue.

Stop all running containers

docker stop $(docker ps -aq) Remove all containers

docker rm $(docker ps -aq) Remove all images

docker rmi $(docker images -q)

查看更多
Deceive 欺骗
5楼-- · 2019-01-13 04:31

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
查看更多
Explosion°爆炸
6楼-- · 2019-01-13 04:38

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
查看更多
Luminary・发光体
7楼-- · 2019-01-13 04:39

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
查看更多
登录 后发表回答