|
| 1 | +from typing import TYPE_CHECKING, Optional |
| 2 | + |
| 3 | +from typing_extensions import Self |
| 4 | + |
| 5 | +from testcontainers.core.docker_client import DockerClient |
| 6 | +from testcontainers.core.utils import setup_logger |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from docker.models.containers import Image |
| 10 | + |
| 11 | +logger = setup_logger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class DockerImage: |
| 15 | + """ |
| 16 | + Basic image object to build Docker images. |
| 17 | +
|
| 18 | + .. doctest:: |
| 19 | +
|
| 20 | + >>> from testcontainers.core.image import DockerImage |
| 21 | +
|
| 22 | + >>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image: |
| 23 | + ... logs = image.get_logs() |
| 24 | +
|
| 25 | + :param tag: Tag for the image to be built (default: None) |
| 26 | + :param path: Path to the Dockerfile to build the image |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + path: str, |
| 32 | + docker_client_kw: Optional[dict] = None, |
| 33 | + tag: Optional[str] = None, |
| 34 | + clean_up: bool = True, |
| 35 | + **kwargs, |
| 36 | + ) -> None: |
| 37 | + self.tag = tag |
| 38 | + self.path = path |
| 39 | + self.id = None |
| 40 | + self._docker = DockerClient(**(docker_client_kw or {})) |
| 41 | + self.clean_up = clean_up |
| 42 | + self._kwargs = kwargs |
| 43 | + |
| 44 | + def build(self, **kwargs) -> Self: |
| 45 | + logger.info(f"Building image from {self.path}") |
| 46 | + docker_client = self.get_docker_client() |
| 47 | + self._image, self._logs = docker_client.build(path=self.path, tag=self.tag, **kwargs) |
| 48 | + logger.info(f"Built image {self.short_id} with tag {self.tag}") |
| 49 | + return self |
| 50 | + |
| 51 | + @property |
| 52 | + def short_id(self) -> str: |
| 53 | + """ |
| 54 | + The ID of the image truncated to 12 characters, without the ``sha256:`` prefix. |
| 55 | + """ |
| 56 | + if self._image.id.startswith("sha256:"): |
| 57 | + return self._image.id.split(":")[1][:12] |
| 58 | + return self._image.id[:12] |
| 59 | + |
| 60 | + def remove(self, force=True, noprune=False) -> None: |
| 61 | + """ |
| 62 | + Remove the image. |
| 63 | +
|
| 64 | + :param force: Remove the image even if it is in use |
| 65 | + :param noprune: Do not delete untagged parent images |
| 66 | + """ |
| 67 | + if self._image and self.clean_up: |
| 68 | + logger.info(f"Removing image {self.short_id}") |
| 69 | + self._image.remove(force=force, noprune=noprune) |
| 70 | + self.get_docker_client().client.close() |
| 71 | + |
| 72 | + def __str__(self) -> str: |
| 73 | + return f"{self.tag if self.tag else self.short_id}" |
| 74 | + |
| 75 | + def __enter__(self) -> Self: |
| 76 | + return self.build() |
| 77 | + |
| 78 | + def __exit__(self, exc_type, exc_val, exc_tb) -> None: |
| 79 | + self.remove() |
| 80 | + |
| 81 | + def get_wrapped_image(self) -> "Image": |
| 82 | + return self._image |
| 83 | + |
| 84 | + def get_docker_client(self) -> DockerClient: |
| 85 | + return self._docker |
| 86 | + |
| 87 | + def get_logs(self) -> list[dict]: |
| 88 | + return list(self._logs) |
0 commit comments