pytest-docker-tools is a pytest plugin that simplifies writing integration tests with Docker containers. It provides fixtures for managing Docker images and containers, allowing tests to easily spin up and tear down isolated environments. As of its latest version 3.1.9, it's actively maintained with a regular release cadence, primarily driven by new features, bug fixes, and compatibility updates.
pip install pytest-docker-toolsVerified import paths — ran on the pinned version, not inferred.
This quickstart defines an `nginx_container` fixture in `conftest.py` that uses the `nginx:alpine` Docker image, mapping port 80. A test then uses this fixture to assert that the Nginx server is reachable and responds with the expected content. This demonstrates defining a container and using it in a test.
Update usage to unpack the tuple: `my_container, my_logs = container_factory(...)` or access `my_container = container_factory(...)[0]`.
Replace `host_ports={'80/tcp': 8080}` and `container_ports=['80/tcp']` with `ports={'80/tcp': 8080}`.Update all references to the `container_logs` fixture to `get_container_logs`.
Upgrade your Python environment to 3.9 or newer.
Ensure Docker Desktop or Docker Engine is running and your user has the necessary permissions to interact with the Docker daemon (e.g., by being in the `docker` group).
Implement robust readiness checks using parameters like `healthcheck_cmd`, `wait_for_response`, `wait_for_log`, or custom code within the fixture to poll the container until it's actually ready.
Create or update your `conftest.py` file in your test suite's root directory with the line: `pytest_plugins = ['pytest_docker_tools']`.
Use the more specific `container.wait_for_output(..., condition=...)` for log conditions, `container.wait_for_port()` for port availability, or other provided waiting utilities like `wait_for_ok`.
Verify the `dockerfile_path` and `context` provided to your `image_source` or `ImageBuilder` definition. Ensure the Dockerfile exists at the specified path relative to the context and that its content is valid Dockerfile syntax.
Ensure the Docker daemon is actively running on your system. Check your user's permissions to access the Docker socket (e.g., add your user to the `docker` group on Linux). Restart the Docker daemon or your system if necessary.