diff --git a/testcontainers/core/container.py b/testcontainers/core/container.py index d5deedbd5..c195a85a9 100644 --- a/testcontainers/core/container.py +++ b/testcontainers/core/container.py @@ -77,17 +77,33 @@ def __del__(self): pass def get_container_host_ip(self) -> str: - # if testcontainers itself runs in docker, get the newly spawned - # container's IP address from the dockder "bridge" network + # infer from docker host + host = self.get_docker_client().host() + if not host: + return "localhost" + + # check testcontainers itself runs inside docker container if inside_container(): - return self.get_docker_client().bridge_ip(self._container.id) - return "localhost" + # 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 host def get_exposed_port(self, port) -> str: + mapped_port = self.get_docker_client().port(self._container.id, port) if inside_container(): - return port - else: - return self.get_docker_client().port(self._container.id, port) + gateway_ip = self.get_docker_client().gateway_ip(self._container.id) + host = self.get_docker_client().host() + + if gateway_ip == host: + return port + return mapped_port def with_command(self, command: str) -> 'DockerContainer': self._command = command diff --git a/testcontainers/core/docker_client.py b/testcontainers/core/docker_client.py index d68ae7c26..7acc1f3e0 100644 --- a/testcontainers/core/docker_client.py +++ b/testcontainers/core/docker_client.py @@ -10,8 +10,12 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import os +import urllib import docker from docker.models.containers import Container +from testcontainers.core.utils import inside_container +from testcontainers.core.utils import default_gateway_ip class DockerClient(object): @@ -42,3 +46,27 @@ def port(self, container_id, port): def bridge_ip(self, container_id): container = self.client.api.containers(filters={'id': container_id})[0] 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'] + + def host(self): + # 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: + return url.hostname + if 'unix' in url.scheme or 'npipe' in url.scheme: + if inside_container(): + ip_address = default_gateway_ip() + if ip_address: + return ip_address + return "localhost" diff --git a/testcontainers/core/utils.py b/testcontainers/core/utils.py index b44ad7635..76d0b4b60 100644 --- a/testcontainers/core/utils.py +++ b/testcontainers/core/utils.py @@ -1,5 +1,6 @@ import os import sys +import subprocess LINUX = "linux" MAC = "mac" @@ -35,3 +36,21 @@ def inside_container(): https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25 """ return os.path.exists('/.dockerenv') + + +def default_gateway_ip(): + """ + Returns gateway IP address of the host that testcontainer process is + running on + + https://github.com/testcontainers/testcontainers-java/blob/3ad8d80e2484864e554744a4800a81f6b7982168/core/src/main/java/org/testcontainers/dockerclient/DockerClientConfigUtils.java#L27 + """ + cmd = ["sh", "-c", "ip route|awk '/default/ { print $3 }'"] + try: + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + ip_address = process.communicate()[0] + if ip_address and process.returncode == 0: + return ip_address.decode('utf-8').strip().strip('\n') + except subprocess.SubprocessError: + return None diff --git a/tests/test_docker_compose.py b/tests/test_docker_compose.py index 6f96253a2..614d56007 100644 --- a/tests/test_docker_compose.py +++ b/tests/test_docker_compose.py @@ -1,8 +1,8 @@ import pytest from testcontainers.compose import DockerCompose +from testcontainers.core.docker_client import DockerClient from testcontainers.core.exceptions import NoSuchPortExposed -from testcontainers.core.utils import inside_container def test_can_spawn_service_via_compose(): @@ -29,5 +29,5 @@ def test_can_throw_exception_if_no_port_exposed(): def test_compose_wait_for_container_ready(): with DockerCompose("tests") as compose: - host = "host.docker.internal" if inside_container() else "localhost" - compose.wait_for("http://%s:4444/wd/hub" % host) + docker = DockerClient() + compose.wait_for("http://%s:4444/wd/hub" % docker.host()) diff --git a/tests/test_new_docker_api.py b/tests/test_new_docker_api.py index adebd05a3..e1ce91058 100644 --- a/tests/test_new_docker_api.py +++ b/tests/test_new_docker_api.py @@ -5,7 +5,6 @@ from testcontainers import mysql from testcontainers.core.generic import GenericContainer -from testcontainers.core.utils import inside_container from importlib import reload @@ -31,10 +30,8 @@ def test_docker_env_variables(): db.with_bind_ports(3306, 32785) with db: url = db.get_connection_url() - if inside_container(): - assert re.match(r'mysql\+pymysql://demo:test@(\d+\.){3}\d+:3306/custom_db', url) - else: - assert url == 'mysql+pymysql://demo:test@localhost:32785/custom_db' + pattern = r'mysql\+pymysql:\/\/demo:test@[\w,.]+:(3306|32785)\/custom_db' + assert re.match(pattern, url) def test_docker_kargs():