Testcontainers Core¶
testcontainers-core is the core functionality for spinning up Docker containers in test environments.
- class DockerContainer(image: str, docker_client_kw: dict | None = None, **kwargs)¶
Basic container object to spin up Docker instances.
>>> from testcontainers.core.container import DockerContainer >>> from testcontainers.core.waiting_utils import wait_for_logs >>> with DockerContainer("hello-world") as container: ... delay = wait_for_logs(container, "Hello from Docker!")
- class DockerImage(path: str | PathLike, docker_client_kw: dict | None = None, tag: str | None = None, clean_up: bool = True, dockerfile_path: str | PathLike = 'Dockerfile', no_cache: bool = False, **kwargs)¶
Basic image object to build Docker images.
>>> from testcontainers.core.image import DockerImage >>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image: ... logs = image.get_logs()
- Parameters:
tag – Tag for the image to be built (default: None)
path – Path to the build context
dockerfile_path – Path to the Dockerfile within the build context path (default: Dockerfile)
no_cache – Bypass build cache; CLI’s –no-cache
- class DbContainer(image: str, docker_client_kw: dict | None = None, **kwargs)¶
DEPRECATED (for removal)
Generic database container.
Examples¶
Using DockerContainer and DockerImage to create a container:
>>> from testcontainers.core.container import DockerContainer
>>> from testcontainers.core.waiting_utils import wait_for_logs
>>> from testcontainers.core.image import DockerImage
>>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-sample:latest") as image:
... with DockerContainer(str(image)) as container:
... delay = wait_for_logs(container, "Test Sample Image")
The DockerImage class is used to build the image from the specified path and tag. The DockerContainer class is then used to create a container from the image.