Skip to content

Commit d8de3dd

Browse files
committed
refactor: move all docker client host detection to docker_client
also use utils for platform detection
1 parent 85d6078 commit d8de3dd

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

core/testcontainers/core/container.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import contextlib
2-
from platform import system
32
from socket import socket
43
from typing import TYPE_CHECKING, Optional, Union
54

@@ -131,11 +130,6 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
131130
def get_container_host_ip(self) -> str:
132131
# infer from docker host
133132
host = self.get_docker_client().host()
134-
if not host:
135-
return "localhost"
136-
# see https://github.com/testcontainers/testcontainers-python/issues/415
137-
if host == "localnpipe" and system() == "Windows":
138-
return "localhost"
139133

140134
# # check testcontainers itself runs inside docker container
141135
# if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):

core/testcontainers/core/docker_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from testcontainers.core.auth import DockerAuthInfo, parse_docker_auth_config
2828
from testcontainers.core.config import testcontainers_config as c
2929
from testcontainers.core.labels import SESSION_ID, create_labels
30-
from testcontainers.core.utils import default_gateway_ip, inside_container, setup_logger
30+
from testcontainers.core.utils import default_gateway_ip, inside_container, is_windows, setup_logger
3131

3232
LOGGER = setup_logger(__name__)
3333

@@ -196,10 +196,12 @@ def host(self) -> str:
196196
return host
197197
try:
198198
url = urllib.parse.urlparse(self.client.api.base_url)
199-
200199
except ValueError:
201200
return "localhost"
202-
if "http" in url.scheme or "tcp" in url.scheme:
201+
if "http" in url.scheme or "tcp" in url.scheme and url.hostname:
202+
# see https://github.com/testcontainers/testcontainers-python/issues/415
203+
if host == "localnpipe" and is_windows():
204+
return "localhost"
203205
return url.hostname
204206
if inside_container() and ("unix" in url.scheme or "npipe" in url.scheme):
205207
ip_address = default_gateway_ip()

0 commit comments

Comments
 (0)