pytest-docker
Simple pytest fixtures for Docker and Docker Compose based tests
What it is and what it does
pytest-docker is a pytest plugin that manages Docker containers as test fixtures. It reads a docker-compose.yml file, starts all defined services before tests run, and tears them down afterward—all handled transparently by pytest's fixture system. You define your services once in compose YAML, then inject a docker_services fixture into your tests to query container ports, wait for readiness, and interact with running services.
The plugin handles the boilerplate of container lifecycle management, port mapping discovery, and health checks. It supports both Docker Compose V2 (default, via the docker compose command) and V1 (via optional extra or custom fixture override). Tests run against real containerized services rather than mocks, making integration tests more realistic and catching issues that unit tests miss.
Use it for:
- Write integration tests against a real database running in a container without manual docker-compose commands.
- Test HTTP services by spinning up a containerized service and making requests to it from pytest.
- Validate multi-service interactions in a single test run using docker-compose to define the stack.
- Debug test failures in an IDE by pinning the compose project name and reusing containers across debug sessions.
- Use multiple docker-compose files to compose different service stacks for different test scenarios.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
pytest-docker provides pytest fixtures that automatically spin up and tear down Docker containers from a docker-compose.yml file for the duration of your test session, eliminating manual container lifecycle management.
Yes. pytest-docker is a well-established plugin (in top 5000 PyPI packages by downloads) with low install friction, permissive licensing, no known vulnerabilities, and active maintenance. It solves a real problem—automating container lifecycle for integration tests—cleanly. Install it if your test suite needs real containerized services rather than mocks or if you're already using docker-compose for local development.
Install
pytest-docker on PyPI
pip
pip install pytest-dockeruv
uv add pytest-dockerpoetry
poetry add pytest-dockerInstalling pytest-docker
Before you install
Low friction: pure Python wheel with only pytest and attrs as runtime dependencies. Maintenance status is aging—last release 275 days ago—but the repository remains active with recent commits and no archived status.
License in practice
MIT license is permissive; safe for commercial and closed-source use without reciprocal obligations.
Quickstart
# Install
pip install pytest-docker
# In conftest.py or test file:
import pytest
@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
return "docker-compose.yml"
# In test:
def test_service(docker_services):
port = docker_services.port_for("myservice", 80)
# Use port to connect to containerized service
Docker and Docker Compose V2 (or V1 via optional extra) must be installed and available in PATH on the test environment.
Verify before relying
- Whether Docker and Docker Compose V2 (or V1 via optional extra) are assumed to be pre-installed on the test environment.
- Performance characteristics when managing many containers or long-running test suites.
- Compatibility with Docker Desktop on Windows and macOS beyond the stated OS classifiers.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — pytest, attrs |
| Maintenance | aging — 275 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,387,633/month — #3,092 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_docker-3.2.5-py3-none-any.whl
Keywords: docker, docker-compose, pytest
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytest-docker-toolsProvides pytest fixture factories for…
permissive · top 5,000 on PyPI
pytest-containerA pytest plugin that automates container…
copyleft · top 15,000 on PyPI
pytest-grpcA pytest plugin that provides fixtures to write…
permissive · top 15,000 on PyPI
pytest-unused-fixturesA pytest plugin that identifies and reports…
permissive · top 15,000 on PyPI
tox-dockerA tox plugin that manages Docker container…
permissive · top 15,000 on PyPI
pytest-shutilProvides pytest fixtures and utilities for…
permissive · top 15,000 on PyPI
docker-composeDocker Compose is a command-line tool for…
permissive · top 5,000 on PyPI
pytest-homeassistant-custom-componentProvides pytest fixtures and testing utilities…
permissive · top 5,000 on PyPI
pytest-printAdds fixtures to pytest that print messages…
permissive · top 15,000 on PyPI
pytest-postgresqlA pytest plugin that provides fixtures to…
copyleft · top 5,000 on PyPI