Testcontainers for Python is a library that provides lightweight, disposable instances of common dependencies (databases, message brokers, web browsers, etc.) for integration testing, leveraging Docker containers. The `testcontainers-core` package, while listed on PyPI, serves as an internal dependency for the main `testcontainers` library, which is the package users should install and interact with directly. The current stable version is 4.14.2, with a regular release cadence including minor and patch updates.
pip install testcontainersVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use a generic `DockerContainer` with a custom command and a `wait_for_log` strategy to ensure the container is ready. The container automatically stops and cleans up upon exiting the `with` block.
Ensure Docker Desktop or a Docker daemon service is running and configured correctly for your environment.
Migrate to `with_wait_for_service()` and use explicit wait strategies like `wait_for_log()`, `wait_for_http()`, `ExecWaitStrategy`, etc. (e.g., `container.with_wait_for_service(wait_for_log("message"))`).Always install `testcontainers` (e.g., `pip install testcontainers`). Imports for core components like `DockerContainer` are still made from `testcontainers.core.container`.
Monitor your system resources during tests. Ensure containers are properly cleaned up (using `with` blocks or `stop()/remove()`) to prevent resource leaks. Consider using smaller base images where possible.
Always pin container image versions (e.g., `mysql:8.0.36`, `alpine:3.17.3`) to ensure your tests are stable and reproducible across different environments and runs.