diff --git a/samcli/lib/build/app_builder.py b/samcli/lib/build/app_builder.py index aded5dbbac0..056af7b53d9 100644 --- a/samcli/lib/build/app_builder.py +++ b/samcli/lib/build/app_builder.py @@ -40,9 +40,10 @@ from samcli.lib.utils import osutils from samcli.lib.utils.packagetype import IMAGE, ZIP from samcli.lib.utils.stream_writer import StreamWriter +from samcli.local.docker.exceptions import ContainerNotStartableException from samcli.local.docker.lambda_build_container import LambdaBuildContainer from samcli.local.docker.utils import is_docker_reachable, get_docker_platform -from samcli.local.docker.manager import ContainerManager +from samcli.local.docker.manager import ContainerManager, DockerImagePullFailedException from samcli.commands._utils.experimental import get_enabled_experimental_flags from samcli.lib.build.exceptions import ( DockerConnectionError, @@ -960,6 +961,10 @@ def _build_function_on_container( # "/." is a Docker thing that instructions the copy command to download contents of the folder only result_dir_in_container = response["result"]["artifacts_dir"] + "/." container.copy(result_dir_in_container, artifacts_dir) + + except DockerImagePullFailedException as ex: + raise BuildInsideContainerError(ex) + finally: self._container_manager.stop(container) diff --git a/tests/unit/lib/build_module/test_app_builder.py b/tests/unit/lib/build_module/test_app_builder.py index efccaf735b6..800c2d7fe29 100644 --- a/tests/unit/lib/build_module/test_app_builder.py +++ b/tests/unit/lib/build_module/test_app_builder.py @@ -28,6 +28,7 @@ from samcli.lib.utils.architecture import X86_64, ARM64 from samcli.lib.utils.packagetype import IMAGE, ZIP from samcli.lib.utils.stream_writer import StreamWriter +from samcli.local.docker.manager import DockerImagePullFailedException from tests.unit.lib.build_module.test_build_graph import generate_function @@ -2724,6 +2725,26 @@ def test_must_raise_on_unsupported_container(self, LambdaBuildContainerMock): self.assertEqual(str(ctx.exception), msg) self.container_manager.stop.assert_called_with(container_mock) + @patch("samcli.lib.build.app_builder.LambdaBuildContainer") + def test_must_raise_on_image_not_found(self, LambdaBuildContainerMock): + config = Mock() + + container_mock = LambdaBuildContainerMock.return_value = Mock() + container_mock.image = "image name" + + self.container_manager.run.side_effect = DockerImagePullFailedException( + f"Could not find {container_mock.image} image locally and failed to pull it from docker." + ) + + with self.assertRaises(BuildInsideContainerError) as ctx: + self.builder._build_function_on_container( + config, "source_dir", "artifacts_dir", "scratch_dir", "manifest_path", "runtime", X86_64, {} + ) + + msg = f"Could not find {container_mock.image} image locally and failed to pull it from docker." + + self.assertEqual(str(ctx.exception), msg) + def test_must_raise_on_docker_not_running(self): config = Mock()