Skip to content

Instantly share code, notes, and snippets.

@simleo
Created March 15, 2019 11:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simleo/10ad923f9d8a2fa410f7ec2d7e96ad57 to your computer and use it in GitHub Desktop.
Save simleo/10ad923f9d8a2fa410f7ec2d7e96ad57 to your computer and use it in GitHub Desktop.
Find dependent images for a given Docker image ID
"""\
Find dependent images for a given Docker image ID.
"""
import argparse
import docker
def find_img(img_idx, id):
try:
return img_idx[id]
except KeyError:
for k, v in img_idx.items():
if k.rsplit(":", 1)[-1].startswith(id):
return v
raise RuntimeError("No image with ID: %s" % id)
def get_children(img_idx):
rval = {}
for img in img_idx.values():
p_id = img.attrs["Parent"]
rval.setdefault(p_id, set()).add(img.id)
return rval
def print_descendants(img_idx, children_map, img_id, indent=0):
children_ids = children_map.get(img_id, [])
for id in children_ids:
child = img_idx[id]
print(" " * indent, id, child.tags)
print_descendants(img_idx, children_map, id, indent=indent+2)
def main(args):
client = docker.from_env()
img_idx = {_.id: _ for _ in client.images.list(all=True)}
img = find_img(img_idx, args.id)
children_map = get_children(img_idx)
print_descendants(img_idx, children_map, img.id)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("id", metavar="IMAGE_ID")
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment