Skip to content

Commit cc02f94

Browse files
authored
fix(core): Add kwargs to image build (#708)
Fix: #706, #614 Now when using kwargs in the Image API, the params are passed correctly into the build ```python with DockerImage(path=dir, tag="test", buildargs={"MY_ARG": "some_arg"}) as image: ``` Added relevant test + updated docstring to better reflect the usage
1 parent ab2a1ab commit cc02f94

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

core/testcontainers/core/image.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ class DockerImage:
2323
>>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image:
2424
... logs = image.get_logs()
2525
26-
:param tag: Tag for the image to be built (default: None)
2726
:param path: Path to the build context
27+
:param docker_client_kw: Keyword arguments to pass to the DockerClient
28+
:param tag: Tag for the image to be built (default: None)
29+
:param clean_up: Remove the image after exiting the context (default: True)
2830
:param dockerfile_path: Path to the Dockerfile within the build context path (default: Dockerfile)
2931
:param no_cache: Bypass build cache; CLI's --no-cache
32+
:param kwargs: Additional keyword arguments to pass to the underlying docker-py
3033
"""
3134

3235
def __init__(
@@ -49,11 +52,11 @@ def __init__(
4952
self._dockerfile_path = dockerfile_path
5053
self._no_cache = no_cache
5154

52-
def build(self, **kwargs) -> Self:
55+
def build(self) -> Self:
5356
logger.info(f"Building image from {self.path}")
5457
docker_client = self.get_docker_client()
5558
self._image, self._logs = docker_client.build(
56-
path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **kwargs
59+
path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **self._kwargs
5760
)
5861
logger.info(f"Built image {self.short_id} with tag {self.tag}")
5962
return self

core/tests/test_image.py

+21
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,24 @@ def test_docker_image_with_custom_dockerfile_path(dockerfile_path: Optional[Path
6464
with DockerContainer(str(image)) as container:
6565
assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch"
6666
assert container.get_logs() == (("Hello world!\n").encode(), b""), "Container logs mismatch"
67+
68+
69+
def test_docker_image_with_kwargs():
70+
with tempfile.TemporaryDirectory() as temp_directory:
71+
with open(f"{temp_directory}/Dockerfile", "w") as f:
72+
f.write(
73+
f"""
74+
FROM alpine:latest
75+
ARG TEST_ARG
76+
ENV TEST_ARG $TEST_ARG
77+
CMD echo $TEST_ARG
78+
"""
79+
)
80+
with DockerImage(
81+
path=temp_directory, tag="test", clean_up=True, no_cache=True, buildargs={"TEST_ARG": "new_arg"}
82+
) as image:
83+
image_short_id = image.short_id
84+
assert image.get_wrapped_image() is not None
85+
with DockerContainer(str(image)) as container:
86+
assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch"
87+
assert container.get_logs() == (("new_arg\n").encode(), b""), "Container logs mismatch"

0 commit comments

Comments
 (0)