Skip to content

Commit ead0f79

Browse files
authored
feat(core): allow custom dockerfile path for image build and bypassing build cache (#615)
fix #610
1 parent 762d2a2 commit ead0f79

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

core/testcontainers/core/image.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class DockerImage:
2424
... logs = image.get_logs()
2525
2626
:param tag: Tag for the image to be built (default: None)
27-
:param path: Path to the Dockerfile to build the image
27+
:param path: Path to the build context
28+
:param dockerfile_path: Path to the Dockerfile within the build context path (default: Dockerfile)
29+
:param no_cache: Bypass build cache; CLI's --no-cache
2830
"""
2931

3032
def __init__(
@@ -33,6 +35,8 @@ def __init__(
3335
docker_client_kw: Optional[dict] = None,
3436
tag: Optional[str] = None,
3537
clean_up: bool = True,
38+
dockerfile_path: Union[str, PathLike] = "Dockerfile",
39+
no_cache: bool = False,
3640
**kwargs,
3741
) -> None:
3842
self.tag = tag
@@ -42,11 +46,15 @@ def __init__(
4246
self._kwargs = kwargs
4347
self._image = None
4448
self._logs = None
49+
self._dockerfile_path = dockerfile_path
50+
self._no_cache = no_cache
4551

4652
def build(self, **kwargs) -> Self:
4753
logger.info(f"Building image from {self.path}")
4854
docker_client = self.get_docker_client()
49-
self._image, self._logs = docker_client.build(path=str(self.path), tag=self.tag, **kwargs)
55+
self._image, self._logs = docker_client.build(
56+
path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **kwargs
57+
)
5058
logger.info(f"Built image {self.short_id} with tag {self.tag}")
5159
return self
5260

core/tests/test_core.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22
import tempfile
33
import random
4+
import os
45

6+
from pathlib import Path
57
from typing import Optional
68

79
from testcontainers.core.container import DockerContainer
@@ -64,3 +66,29 @@ def test_docker_image(test_image_tag: Optional[str], test_cleanup: bool, check_f
6466
assert container.get_logs() == ((random_string + "\n").encode(), b""), "Container logs mismatch"
6567

6668
check_for_image(image_short_id, test_cleanup)
69+
70+
71+
@pytest.mark.parametrize("dockerfile_path", [None, Path("subdir/my.Dockerfile")])
72+
def test_docker_image_with_custom_dockerfile_path(dockerfile_path: Optional[Path]):
73+
with tempfile.TemporaryDirectory() as temp_directory:
74+
temp_dir_path = Path(temp_directory)
75+
if dockerfile_path:
76+
os.makedirs(temp_dir_path / dockerfile_path.parent, exist_ok=True)
77+
dockerfile_rel_path = dockerfile_path
78+
dockerfile_kwargs = {"dockerfile_path": dockerfile_path}
79+
else:
80+
dockerfile_rel_path = Path("Dockerfile") # default
81+
dockerfile_kwargs = {}
82+
83+
with open(temp_dir_path / dockerfile_rel_path, "x") as f:
84+
f.write(
85+
f"""
86+
FROM alpine:latest
87+
CMD echo "Hello world!"
88+
"""
89+
)
90+
with DockerImage(path=temp_directory, tag="test", clean_up=True, no_cache=True, **dockerfile_kwargs) as image:
91+
image_short_id = image.short_id
92+
with DockerContainer(str(image)) as container:
93+
assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch"
94+
assert container.get_logs() == (("Hello world!\n").encode(), b""), "Container logs mismatch"

0 commit comments

Comments
 (0)