-
-
Save xkou/b7fd52a3cd937f0488c105803c0fc4c0 to your computer and use it in GitHub Desktop.
view docker image dependence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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