Skip to content

Conversation

@am1ter
Copy link
Contributor

@am1ter am1ter commented Jun 14, 2025

Description

  • Added the container_host parameter to DockerService for flexibility. It defaults to 127.0.0.1.
  • Introduced a new postgres_host fixture to retrieve the host from environment variables if it is set.
  • Updated PostgreSQL service functions to use the new host parameter.
  • I did not update other database services, because I am not sure what the best way to set the host address for them is.

Closes

This PR partially fixes #86.

I use docker-in-docker and see an error ValueError: Service 'pytest_databases_postgres' failed to come online when I try to run unit tests.

The root cause of the issue when using this library in the Docker-in-Docker case is the hardcoded 127.0.0.1 value as the container host. For Docker-in-Docker, it is necessary to use the host.docker.internal value. This PR allows setting the POSTGRES_HOST environment variable.

P.S. @cofin @peterHoburg @provinzkraut Thank you very much for this library! It seems to me more cool than testcontainers I used before.

- Added `container_host` parameter to `DockerService` for flexibility.
- Introduced `postgres_host` fixture to retrieve host from environment.
- Updated PostgreSQL service functions to utilize the new host
  parameter.
@am1ter am1ter changed the title Enhance Postgres Services with configurable container host feat: Enhance Postgres Services with configurable container host Jun 14, 2025
Copy link
Member

@cofin cofin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change! LGTM

@cofin cofin merged commit bc02b8d into litestar-org:main Jun 14, 2025
14 checks passed
@RaiseRuntimeError
Copy link

I appreciate the effort to make pytest-databases more flexible in Docker-in-Docker (DinD) environments. That said, I don’t think this change is a great fit in its current form.

This PR introduces custom configuration only for PostgreSQL, which diverges from the general architecture of the library and leads to inconsistent behavior across database services. Specifically:

  • The logic for resolving the Docker host (container_host) has now been duplicated and hardcoded just for Postgres.
  • Other services like MySQL, Redis, or Mongo would still break in the same DinD scenarios unless each one is updated individually — which increases maintenance overhead and breaks consistency.
  • There’s already a more general solution in _service.py using get_docker_client and get_docker_host, which could be extended to compute a reasonable default for container_host.

For example, the following approach works across all services, not just Postgres:

parsed = urllib.parse.urlparse(self._client.api.base_url)
host = parsed.hostname if parsed.scheme in ("tcp", "http", "https") else "127.0.0.1"
service = ServiceContainer(host=host, port=host_port)

This would eliminate the need for a POSTGRES_HOST env var and the special postgres_host fixture altogether, while making the library more robust in varied Docker environments (e.g., DinD, Podman, remote Docker hosts).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support docker-in-docker

3 participants