nicocesar nicocesar - 1 month ago 51
Linux Question

docker how can I get the list of dependent child images?

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.

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 -q)

Look for images with parent id starting with f50f9524513f, then look for child images of those, etc..

The since filter is new in docker 1.12

Here's a python3 script to parse the docker output and generate the list you want:

#!/usr/bin/python3
import re
import sys

def desc(image_ids, links):
    if links:
        (image_id, parent_id), *tail = links
        checkid = lambda i: parent_id.startswith(i)
        if any(map(checkid, image_ids)):
            return desc(image_ids | {image_id}, 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())
    print('\n'.join(desc(image_ids, links)))

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

docker inspect --format='{{.Id}} {{.Parent}}' $(docker images --filter since=f50f9524513f -q) | python3 desc.py f50f9524513f
Comments