diff --git a/core/testcontainers/core/image.py b/core/testcontainers/core/image.py index 6d793f83..27696619 100644 --- a/core/testcontainers/core/image.py +++ b/core/testcontainers/core/image.py @@ -23,10 +23,13 @@ class DockerImage: >>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image: ... logs = image.get_logs() - :param tag: Tag for the image to be built (default: None) :param path: Path to the build context + :param docker_client_kw: Keyword arguments to pass to the DockerClient + :param tag: Tag for the image to be built (default: None) + :param clean_up: Remove the image after exiting the context (default: True) :param dockerfile_path: Path to the Dockerfile within the build context path (default: Dockerfile) :param no_cache: Bypass build cache; CLI's --no-cache + :param kwargs: Additional keyword arguments to pass to the underlying docker-py """ def __init__( @@ -49,11 +52,11 @@ def __init__( self._dockerfile_path = dockerfile_path self._no_cache = no_cache - def build(self, **kwargs) -> Self: + def build(self) -> Self: logger.info(f"Building image from {self.path}") docker_client = self.get_docker_client() self._image, self._logs = docker_client.build( - path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **kwargs + path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **self._kwargs ) logger.info(f"Built image {self.short_id} with tag {self.tag}") return self diff --git a/core/tests/test_image.py b/core/tests/test_image.py index da35eda0..bff49618 100644 --- a/core/tests/test_image.py +++ b/core/tests/test_image.py @@ -64,3 +64,24 @@ def test_docker_image_with_custom_dockerfile_path(dockerfile_path: Optional[Path with DockerContainer(str(image)) as container: assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch" assert container.get_logs() == (("Hello world!\n").encode(), b""), "Container logs mismatch" + + +def test_docker_image_with_kwargs(): + with tempfile.TemporaryDirectory() as temp_directory: + with open(f"{temp_directory}/Dockerfile", "w") as f: + f.write( + f""" + FROM alpine:latest + ARG TEST_ARG + ENV TEST_ARG $TEST_ARG + CMD echo $TEST_ARG + """ + ) + with DockerImage( + path=temp_directory, tag="test", clean_up=True, no_cache=True, buildargs={"TEST_ARG": "new_arg"} + ) as image: + image_short_id = image.short_id + assert image.get_wrapped_image() is not None + with DockerContainer(str(image)) as container: + assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch" + assert container.get_logs() == (("new_arg\n").encode(), b""), "Container logs mismatch"