From ab426832d173889603d4f0099011e4886ae44df2 Mon Sep 17 00:00:00 2001 From: Tuvshinbayar Date: Fri, 29 May 2020 09:20:44 +0800 Subject: [PATCH 1/4] fallback to gateway address --- testcontainers/core/container.py | 1 + 1 file changed, 1 insertion(+) diff --git a/testcontainers/core/container.py b/testcontainers/core/container.py index c195a85a9..3ee98e399 100644 --- a/testcontainers/core/container.py +++ b/testcontainers/core/container.py @@ -93,6 +93,7 @@ def get_container_host_ip(self) -> str: 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: From 53cf39ad7299f8ba3b35b7380c5ae145370317f2 Mon Sep 17 00:00:00 2001 From: Tuvshinbayar Davaa Date: Sun, 24 Oct 2021 10:02:44 +0200 Subject: [PATCH 2/4] Add support for docker internal host --- testcontainers/core/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/testcontainers/core/utils.py b/testcontainers/core/utils.py index fe14d724d..960ebbc01 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" @@ -47,6 +48,17 @@ 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(): """ From 39258ac2338a461ba80cbc373a5815fd1f0a37dd Mon Sep 17 00:00:00 2001 From: Tuvshinbayar Davaa Date: Sun, 24 Oct 2021 10:56:38 +0200 Subject: [PATCH 3/4] improve host detection --- testcontainers/core/container.py | 23 ++++++------------ testcontainers/core/docker_client.py | 36 +++++++++++++++++++++------- 2 files changed, 35 insertions(+), 24 deletions(-) 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..eee314a63 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: + return default_gateway_ip() + + def resolve_host(self, container_id, default="localhost"): + # get address for host.docker.internal if supports + host = docker_internal_host() + + 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): + 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" From 94209d1506d3606ca84386c6c5c23a1acbe4fa6a Mon Sep 17 00:00:00 2001 From: Tuvshinbayar Davaa Date: Sun, 24 Oct 2021 11:41:37 +0200 Subject: [PATCH 4/4] Fix linting --- testcontainers/core/docker_client.py | 4 ++-- testcontainers/core/utils.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/testcontainers/core/docker_client.py b/testcontainers/core/docker_client.py index eee314a63..598fba354 100644 --- a/testcontainers/core/docker_client.py +++ b/testcontainers/core/docker_client.py @@ -52,9 +52,9 @@ def gateway_ip(self, container_id): try: container = self.client.api.containers(filters={'id': container_id})[0] return container['NetworkSettings']['Networks']['bridge']['Gateway'] - except: + 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() diff --git a/testcontainers/core/utils.py b/testcontainers/core/utils.py index 960ebbc01..6553b289c 100644 --- a/testcontainers/core/utils.py +++ b/testcontainers/core/utils.py @@ -48,10 +48,11 @@ 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 """ @@ -60,6 +61,7 @@ def docker_internal_host(): except socket.gaierror: return None + def default_gateway_ip(): """ Returns gateway IP address of the host that testcontainer process is