Skip to content

Commit

Permalink
refactor: move all docker client host detection to docker_client
Browse files Browse the repository at this point in the history
also use utils for platform detection
  • Loading branch information
CarliJoy committed Oct 11, 2024
1 parent 85d6078 commit d8de3dd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
6 changes: 0 additions & 6 deletions core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
from platform import system
from socket import socket
from typing import TYPE_CHECKING, Optional, Union

Expand Down Expand Up @@ -131,11 +130,6 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
def get_container_host_ip(self) -> str:
# infer from docker host
host = self.get_docker_client().host()
if not host:
return "localhost"
# see https://github.com/testcontainers/testcontainers-python/issues/415
if host == "localnpipe" and system() == "Windows":
return "localhost"

# # check testcontainers itself runs inside docker container
# if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):
Expand Down
8 changes: 5 additions & 3 deletions core/testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from testcontainers.core.auth import DockerAuthInfo, parse_docker_auth_config
from testcontainers.core.config import testcontainers_config as c
from testcontainers.core.labels import SESSION_ID, create_labels
from testcontainers.core.utils import default_gateway_ip, inside_container, setup_logger
from testcontainers.core.utils import default_gateway_ip, inside_container, is_windows, setup_logger

LOGGER = setup_logger(__name__)

Expand Down Expand Up @@ -196,10 +196,12 @@ def host(self) -> str:
return host
try:
url = urllib.parse.urlparse(self.client.api.base_url)

except ValueError:
return "localhost"
if "http" in url.scheme or "tcp" in url.scheme:
if "http" in url.scheme or "tcp" in url.scheme and url.hostname:
# see https://github.com/testcontainers/testcontainers-python/issues/415
if host == "localnpipe" and is_windows():
return "localhost"
return url.hostname
if inside_container() and ("unix" in url.scheme or "npipe" in url.scheme):
ip_address = default_gateway_ip()
Expand Down

0 comments on commit d8de3dd

Please sign in to comment.