Use Docker Compose with Testcontainers
maintestcontainers.compose.compose module to leverage Docker Compose functionality, allowing you to spin up multi-container environments defined in Compose files.repository·main·Indexed 25 days ago
https://github.com/testcontainers/testcontainers-pythonA Python library for managing throwaway instances of any service that can run in a Docker container, facilitating functional and integration testing. It provides programmatic control over container lifecycles and includes community modules for common services such as PostgreSQL, Kafka, LocalStack, Elasticsearch, Redis, and various cloud emulators for AWS, Azure, and Google Cloud.
testcontainers.compose.compose module to leverage Docker Compose functionality, allowing you to spin up multi-container environments defined in Compose files.DockerRegistryContainer provides a way to spin up a local registry container, allowing you to push your built images to a local endpoint for testing within your test suite.Testcontainers for Python uses a specialized container called "Ryuk" (the reaper) to ensure resources are cleaned up. Ryuk is automatically started when you create your first test container. It is designed to remove containers even in scenarios where standard Python cleanup might fail, such as:
Ryuk tracks containers using a unique session ID assigned to each test session, which enables safe cleanup even when running tests in parallel.
Testcontainers-Python supports three connection modes to determine how containers are accessed. You can set this via the TESTCONTAINERS_CONNECTION_MODE environment variable or the connection.mode property in .testcontainers.properties.
Supported modes:
bridge_ip (default): Uses the bridge network IP address.gateway_ip: Uses the gateway IP address.docker_host: Uses the Docker host address.Testcontainers-Python attempts to automatically detect the Docker environment. If automatic detection fails, it follows this specific precedence order for customization:
tc.host property in ~/.testcontainers.properties (e.g., tc.host=tcp://my.docker.host:1234)TC_HOST or TESTCONTAINERS_HOST_OVERRIDE environment variableDOCKER_HOST environment variabledocker context use)/var/run/docker.sock)docker.host property in ~/.testcontainers.properties (e.g., docker.host=tcp://my.docker.host:1234)${XDG_RUNTIME_DIR}/.docker/run/docker.sock, ${HOME}/.docker/run/docker.sock, etc.)If no host is detected, a DockerHostError is raised.
Testcontainers-Python supports three connection modes to determine how containers are accessed. This is useful for configuring how your test code interacts with containers in different environments like Docker-in-Docker (DinD) or local development.
bridge_ip (default): Uses the bridge network IP address. Best for DinD scenarios, direct container-to-container communication, or when containers need to communicate over a bridge network.gateway_ip: Uses the gateway IP address. Best for DinD scenarios or when containers need to access the host network/services running on the host.docker_host: Uses the Docker host address. Best for local development or when running tests outside of containers to access containers from the host machine.You can set the connection mode using the TESTCONTAINERS_CONNECTION_MODE environment variable or the connection.mode property in a .testcontainers.properties file.
core module to craft custom containers. For common generic use cases (like running a FastAPI server), use the testcontainers-generic module (e.g., ServerContainer).Testcontainers-Python automatically detects the Docker socket path. If customization is needed, it follows this order:
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE environment variable (used by Ryuk, Docker Compose, etc.)//var/run/docker.sock UNC path. Otherwise, it returns the default rootless Docker socket path.unix:// schema, the schema is removed./var/run/docker.sock is used as a fallback.If the host cannot be discovered, a DockerHostError is raised.
There are three primary ways to enable communication between containers:
with_network_aliases(["alias"]). Other containers on the same network can reach it via alias:port.get_container_host_ip() to retrieve the IP address of a container for direct communication.with_network_mode("host") to make the container use the host's network stack directly.To use the CrateDB module with Testcontainers, install it along with its required dependencies (sqlalchemy and sqlalchemy-cratedb) using the following command:
pip install testcontainers[cratedb] sqlalchemy sqlalchemy-cratedbTestcontainers Python allows you to use Docker containers for functional and integration testing. You can spin up specific database containers (like PostgreSQL) and use convenience methods like get_connection_url() to connect your application code to the running container.
>>> from testcontainers.postgres import PostgresContainer
>>> import sqlalchemy
>>> with PostgresContainer("postgres:16") as postgres:
... engine = sqlalchemy.create_engine(postgres.get_connection_url())
... with engine.begin() as connection:
... result = connection.execute(sqlalchemy.text("select version()"))
... version, = result.fetchone()
>>> version
'PostgreSQL 16...'Testcontainers-Python can automatically use credentials already stored in your Docker credential store. This is the simplest method for accessing private registries if you have already performed a docker login on your machine.
from testcontainers.community.generic import GenericContainer
# Docker will automatically use stored credentials
container = GenericContainer("private.registry.com/myimage:latest")