def build()

in samcli/local/docker/lambda_image.py [0:0]


    def build(self, runtime, packagetype, image, layers, architecture, stream=None, function_name=None):
        """
        Build the image if one is not already on the system that matches the runtime and layers

        Parameters
        ----------
        runtime str
            Name of the Lambda runtime
        packagetype str
            Packagetype for the Lambda
        image str
            Pre-defined invocation image.
        layers list(samcli.commands.local.lib.provider.Layer)
            List of layers
        architecture
            Architecture type either x86_64 or arm64 on AWS lambda
        function_name str
            The name of the function that the image is building for

        Returns
        -------
        str
            The image to be used (REPOSITORY:TAG)
        """
        image_name = None

        if packagetype == IMAGE:
            image_name = image
        elif packagetype == ZIP:
            if self.invoke_images:
                image_name = self.invoke_images.get(function_name, self.invoke_images.get(None))
            if not image_name:
                tag_name = f"latest-{architecture}" if has_runtime_multi_arch_image(runtime) else "latest"
                image_name = f"{self._INVOKE_REPO_PREFIX}-{runtime}:{tag_name}"

        if not image_name:
            raise InvalidIntermediateImageError(f"Invalid PackageType, PackageType needs to be one of [{ZIP}, {IMAGE}]")

        if image:
            self.skip_pull_image = True

        # Default image tag to be the base image with a tag of 'rapid' instead of latest.
        # If the image name had a digest, removing the @ so that a valid image name can be constructed
        # to use for the local invoke image name.
        image_repo = image_name.split(":")[0].replace("@", "")
        image_tag = f"{image_repo}:{RAPID_IMAGE_TAG_PREFIX}-{version}-{architecture}"

        downloaded_layers = []

        if layers and packagetype == ZIP:
            downloaded_layers = self.layer_downloader.download_all(layers, self.force_image_build)

            docker_image_version = self._generate_docker_image_version(downloaded_layers, runtime, architecture)
            image_tag = f"{self._SAM_CLI_REPO_NAME}:{docker_image_version}"

        image_not_found = False

        # If we are not using layers, build anyways to ensure any updates to rapid get added
        try:
            self.docker_client.images.get(image_tag)
        except docker.errors.ImageNotFound:
            LOG.info("Image was not found.")
            image_not_found = True

        # If building a new rapid image, delete older rapid images of the same repo
        if image_not_found and image_tag == f"{image_repo}:{RAPID_IMAGE_TAG_PREFIX}-{version}-{architecture}":
            self._remove_rapid_images(image_repo)

        if (
            self.force_image_build
            or image_not_found
            or any(layer.is_defined_within_template for layer in downloaded_layers)
            or not runtime
        ):
            stream_writer = stream or StreamWriter(sys.stderr)
            stream_writer.write("Building image...")
            stream_writer.flush()
            self._build_image(
                image if image else image_name, image_tag, downloaded_layers, architecture, stream=stream_writer
            )

        return image_tag