Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Add kwargs to image build #708

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
9 changes: 6 additions & 3 deletions core/testcontainers/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions core/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"