Skip to content

Commit

Permalink
fix: on windows, DockerCompose.get_service_host returns an unusable "…
Browse files Browse the repository at this point in the history
…0.0.0.0" - adjust to 127.0.0.1 (#457)

#358 

not sure if this is the right solution
  • Loading branch information
alexanderankin committed May 25, 2024
1 parent 59fbcfa commit 2aa3d37
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass, field, fields
from dataclasses import asdict, dataclass, field, fields
from functools import cached_property
from json import loads
from os import PathLike
from platform import system
from re import split
from subprocess import CompletedProcess
from subprocess import run as subprocess_run
Expand Down Expand Up @@ -38,6 +39,14 @@ class PublishedPort:
PublishedPort: Optional[str] = None
Protocol: Optional[str] = None

def normalize(self):
url_not_usable = system() == "Windows" and self.URL == "0.0.0.0"
if url_not_usable:
self_dict = asdict(self)
self_dict.update({"URL": "127.0.0.1"})
return PublishedPort(**self_dict)
return self


OT = TypeVar("OT")

Expand Down Expand Up @@ -357,7 +366,7 @@ def get_service_port(
str:
The mapped port on the host
"""
return self.get_container(service_name).get_publisher(by_port=port).PublishedPort
return self.get_container(service_name).get_publisher(by_port=port).normalize().PublishedPort

def get_service_host(
self,
Expand All @@ -379,14 +388,14 @@ def get_service_host(
str:
The hostname for the service
"""
return self.get_container(service_name).get_publisher(by_port=port).URL
return self.get_container(service_name).get_publisher(by_port=port).normalize().URL

def get_service_host_and_port(
self,
service_name: Optional[str] = None,
port: Optional[int] = None,
):
publisher = self.get_container(service_name).get_publisher(by_port=port)
publisher = self.get_container(service_name).get_publisher(by_port=port).normalize()
return publisher.URL, publisher.PublishedPort

@wait_container_is_ready(HTTPError, URLError)
Expand Down

0 comments on commit 2aa3d37

Please sign in to comment.