<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _build_image(self, base_image, docker_tag, layers):
 """ Builds the image Parameters base_image str Base Image to use for the new image docker_tag Docker tag (REPOSITORY:TAG) to use when building the image layers list(samcli.commands.local.lib.provider.Layer) List of Layers to be use to mount in the image Returns ------- None Raises ------ samcli.commands.local.cli_common.user_exceptions.ImageBuildException When docker fails to build the image """

        dockerfile_content = self._generate_dockerfile(base_image, layers)

        # Create dockerfile in the same directory of the layer cache
        dockerfile_name = "dockerfile_" + str(uuid.uuid4())
        full_dockerfile_path = Path(self.layer_downloader.layer_cache, dockerfile_name)

        try:
            with open(str(full_dockerfile_path), "w") as dockerfile:
                dockerfile.write(dockerfile_content)

            tar_paths = {str(full_dockerfile_path): "Dockerfile"}
            for layer in layers:
                tar_paths[layer.codeuri] = '/' + layer.name

            with create_tarball(tar_paths) as tarballfile:
                try:
                    self.docker_client.images.build(fileobj=tarballfile,
                                                    custom_context=True,
                                                    rm=True,
                                                    tag=docker_tag,
                                                    pull=not self.skip_pull_image)
                except (docker.errors.BuildError, docker.errors.APIError):
                    LOG.exception("Failed to build Docker Image")
                    raise ImageBuildException("Building Image failed.")
        finally:
            if full_dockerfile_path.exists():
                full_dockerfile_path.unlink()