How to Get the List of Dependent Child Images in Docker

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

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.

What are docker child images

So from your question, assuming that your workflow has been to start a container, work interactively inside the container and then commit the changes to a new image, the answer is that what you're essentially doing is creating a new layer on top of the existing kali base image.

As such the full stack of layers are required to operate. This doesn't mean that the disk space taken is 2.25+1.12+1.07 however as Docker shares the lower layers.

That said this isn't a great way to create Docker images, as doing things like chown and mv can leave redundant files in the image.

A better way is to create a new Dockerfile based on the original kali image (using FROM kali:latest in the Dockerfile) and then make the changes you want in the Dockerfile and execute a build , to give you the final image.

There's more information on Docker's website here

Multiple docker images after building image from Dockerfile

The <none:none> images are the intermediate layers resulted from your docker build.

Docker image is composed of layers. Each instruction in your Dockerfile results in a layer. These layers are re-used between different images. This results in efficient usage of disk space. So if a is layer being used by another image, you won't be able to delete the layer.

You can run the following command which will remove the layers which are not referenced in any of the images. These layers are called dangling.

docker rmi $(docker images -f "dangling=true" -q)

https://www.projectatomic.io/blog/2015/07/what-are-docker-none-none-images/

How to delete parent image after committing to a child image in docker?

Container is formed with different docker image layers as follows.

So when you add new things to container, and finally make it as another image, you just add a new layer upon the original image layers, so no need to delete the base image layers, they are shared by your new containers. Also, only with these base image layers, your new image with its own layers can be possible to form a new container.

Sample Image



Related Topics



Leave a reply



Submit