Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. import docker
  5. import jsondiff as jd
  6. import re
  7. import uuid
  8.  
  9.  
  10. CLIENT = docker.from_env(timeout=600)
  11.  
  12. def gen_dockerfile(container_id):
  13. contents = []
  14. container = CLIENT.containers.get(container_id)
  15. # FROM
  16. contents.append("FROM {}".format(container.attrs.get("Config").get("Image")))
  17. # RUN
  18. contents.extend(get_run_command(container))
  19. # write Dockerfile
  20. with open("Dockerfile", "w", encoding="utf-8") as f:
  21. f.write("\n".join(contents))
  22.  
  23.  
  24. def get_run_command(container):
  25. runs = []
  26. # get diff of images' history
  27. tmp_image_name = container.name + str(uuid.uuid4())
  28. tmp_image = container.commit(tmp_image_name)
  29. base_image = CLIENT.images.get(container.attrs.get("Config").get("Image"))
  30. commands = sorted(
  31. jd.diff(base_image.history(), tmp_image.history()).get(jd.insert), key=lambda x: x[1].get("Created")
  32. )
  33. for command in commands:
  34. created_by = command[1].get("CreatedBy")
  35. runs.append("RUN {}".format(created_by))
  36. # delete tmp image
  37. CLIENT.images.remove(tmp_image_name)
  38. return runs
  39.  
  40. if __name__ == "__main__":
  41. container_id = input(">> ")
  42. gen_dockerfile(container_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement