diff --git a/testcontainers/core/container.py b/testcontainers/core/container.py index 0ca126ca6..1eb951409 100644 --- a/testcontainers/core/container.py +++ b/testcontainers/core/container.py @@ -78,32 +78,23 @@ def __del__(self): def get_container_host_ip(self) -> str: # infer from docker host - host = self.get_docker_client().host() + host = self.get_docker_client().host(self._container.id) if not host: return "localhost" - # check testcontainers itself runs inside docker container - if inside_container(): - # If newly spawned container's gateway IP address from the docker - # "bridge" network is equal to detected host address, we should use - # container IP address, otherwise fall back to detected host - # address. Even it's inside container, we need to double check, - # because docker host might be set to docker:dind, usually in CI/CD environment - gateway_ip = self.get_docker_client().gateway_ip(self._container.id) - - if gateway_ip == host: - return self.get_docker_client().bridge_ip(self._container.id) - return gateway_ip return host def get_exposed_port(self, port) -> str: mapped_port = self.get_docker_client().port(self._container.id, port) + if inside_container(): - gateway_ip = self.get_docker_client().gateway_ip(self._container.id) - host = self.get_docker_client().host() + bridge_ip = self.get_docker_client().bridge_ip(self._container.id) + host = self.get_docker_client().host(self._container.id) - if gateway_ip == host: + # use exposed port if host detection returns bridge ip + if bridge_ip == host: return port + return mapped_port def with_command(self, command: str) -> 'DockerContainer': diff --git a/testcontainers/core/docker_client.py b/testcontainers/core/docker_client.py index 7acc1f3e0..598fba354 100644 --- a/testcontainers/core/docker_client.py +++ b/testcontainers/core/docker_client.py @@ -15,6 +15,7 @@ import docker from docker.models.containers import Container from testcontainers.core.utils import inside_container +from testcontainers.core.utils import docker_internal_host from testcontainers.core.utils import default_gateway_ip @@ -48,25 +49,44 @@ def bridge_ip(self, container_id): return container['NetworkSettings']['Networks']['bridge']['IPAddress'] def gateway_ip(self, container_id): - container = self.client.api.containers(filters={'id': container_id})[0] - return container['NetworkSettings']['Networks']['bridge']['Gateway'] + try: + container = self.client.api.containers(filters={'id': container_id})[0] + return container['NetworkSettings']['Networks']['bridge']['Gateway'] + except Exception: + return default_gateway_ip() + + def resolve_host(self, container_id, default="localhost"): + # get address for host.docker.internal if supports + host = docker_internal_host() - def host(self): + try: + if not host: + return self.gateway_ip(container_id) + if host == self.gateway_ip(container_id): + return self.bridge_ip(container_id) + return host + except Exception: + return default + + def host(self, container_id): # https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644 # if os env TC_HOST is set, use it host = os.environ.get('TC_HOST') if host: return host + try: url = urllib.parse.urlparse(self.client.api.base_url) - except ValueError: return None + if 'http' in url.scheme or 'tcp' in url.scheme: + # check testcontainers itself runs inside docker container + if url.hostname == "localhost" and inside_container(): + return self.resolve_host(container_id) return url.hostname if 'unix' in url.scheme or 'npipe' in url.scheme: + # check testcontainers itself runs inside docker container if inside_container(): - ip_address = default_gateway_ip() - if ip_address: - return ip_address - return "localhost" + return self.resolve_host(container_id) + return "localhost" diff --git a/testcontainers/core/utils.py b/testcontainers/core/utils.py index fe14d724d..6553b289c 100644 --- a/testcontainers/core/utils.py +++ b/testcontainers/core/utils.py @@ -2,6 +2,7 @@ import sys import subprocess import logging +import socket LINUX = "linux" MAC = "mac" @@ -48,6 +49,19 @@ def inside_container(): return os.path.exists('/.dockerenv') +def docker_internal_host(): + """ + Returns resolved ip address if docker host supports host.docker.internal + + https://github.com/moby/moby/pull/40007 + https://github.com/moby/libnetwork/pull/2348 + """ + try: + return socket.gethostbyname('host.docker.internal') + except socket.gaierror: + return None + + def default_gateway_ip(): """ Returns gateway IP address of the host that testcontainer process is