Troubleshooting
This section describes common errors you might encounter working with Testcontainers in the Python environment.
- Error on running tests with pytest:
ScopeMismatch: You tried to access the function scoped fixture event_loop with a session scoped request object, involved factories. -
Problem: the error occurs when you're using asynchronous fixtures with a scope higher than
function, e.g., fixturemoto_containerhassessionscope. The defaultevent_loopfixture provided bypytest-asynciois a function-scoped fixture, so it can't be used with session-scoped fixtures. -
Solution: override the
event_loopfixture with a session-scoped fixture by placing it in your project's defaultconftest.py.
- Error when using a Docker Desktop alternative (Colima, Rancher Desktop, Podman, OrbStack):
docker.errors.DockerException: Error while fetching server API version- or
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. -
Problem: Testcontainers connects to Docker through
docker.from_env(), which reads theDOCKER_HOSTenvironment variable but does not read the Docker CLI context (docker context use ...). Docker Desktop alternatives usually place their socket somewhere other than/var/run/docker.sock, so even though thedockerCLI works, Testcontainers tries the default socket and fails to connect. -
Solution: point
DOCKER_HOSTat the active runtime's socket. The most portable way is to derive it from the active Docker context:Or set it explicitly for your runtime, for example:
# Colima export DOCKER_HOST="unix://$HOME/.colima/default/docker.sock" # Rancher Desktop export DOCKER_HOST="unix://$HOME/.rd/docker.sock" # OrbStack export DOCKER_HOST="unix://$HOME/.orbstack/run/docker.sock"Add the export to your shell profile (or set it via pytest-env) so it is present whenever you run
pytest.