def patch_dockerfile(dockerfile, images): """ Patch an existing Dockerfile to replace FROM image statements by their local images with digests. It supports multi-stage images This is needed to avoid retrieving remote images before checking current img state Bug https://github.com/genuinetools/img/issues/206 """ assert os.path.exists(dockerfile), "Missing dockerfile {}".format( dockerfile) assert isinstance(images, list) if not images: return def _find_replacement(original): # Replace an image name by its local version # when it exists repo, tag = parse_image_name(original) for image in images: if image["repository"] == repo and image["tag"] == tag: if image["registry"]: local = "{}/{}@sha256:{}".format(image["registry"], image["repository"], image["digest"]) else: local = "{}@sha256:{}".format(image["repository"], image["digest"]) logger.info("Replacing image {} by {}".format(original, local)) return local return original # Parse the given dockerfile and update its parent images # with local version given by current img state # The FROM statement parsing & replacement is provided # by the DockerfileParser parser = DockerfileParser() parser.dockerfile_path = dockerfile parser.content = open(dockerfile).read() logger.info("Initial parent images: {}".format(" & ".join( parser.parent_images))) parser.parent_images = list(map(_find_replacement, parser.parent_images))
def get_image_base(self, image, tag): abs_image_path = self.get_abs_image_dockerfile(image, tag) doc = DockerfileParser() doc.dockerfile_path = abs_image_path return doc.baseimage