Skip to content

Commit 2aa3d37

Browse files
authoredMay 25, 2024··
fix: on windows, DockerCompose.get_service_host returns an unusable "0.0.0.0" - adjust to 127.0.0.1 (#457)
#358 not sure if this is the right solution
1 parent 59fbcfa commit 2aa3d37

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed
 

‎core/testcontainers/compose/compose.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from dataclasses import dataclass, field, fields
1+
from dataclasses import asdict, dataclass, field, fields
22
from functools import cached_property
33
from json import loads
44
from os import PathLike
5+
from platform import system
56
from re import split
67
from subprocess import CompletedProcess
78
from subprocess import run as subprocess_run
@@ -38,6 +39,14 @@ class PublishedPort:
3839
PublishedPort: Optional[str] = None
3940
Protocol: Optional[str] = None
4041

42+
def normalize(self):
43+
url_not_usable = system() == "Windows" and self.URL == "0.0.0.0"
44+
if url_not_usable:
45+
self_dict = asdict(self)
46+
self_dict.update({"URL": "127.0.0.1"})
47+
return PublishedPort(**self_dict)
48+
return self
49+
4150

4251
OT = TypeVar("OT")
4352

@@ -357,7 +366,7 @@ def get_service_port(
357366
str:
358367
The mapped port on the host
359368
"""
360-
return self.get_container(service_name).get_publisher(by_port=port).PublishedPort
369+
return self.get_container(service_name).get_publisher(by_port=port).normalize().PublishedPort
361370

362371
def get_service_host(
363372
self,
@@ -379,14 +388,14 @@ def get_service_host(
379388
str:
380389
The hostname for the service
381390
"""
382-
return self.get_container(service_name).get_publisher(by_port=port).URL
391+
return self.get_container(service_name).get_publisher(by_port=port).normalize().URL
383392

384393
def get_service_host_and_port(
385394
self,
386395
service_name: Optional[str] = None,
387396
port: Optional[int] = None,
388397
):
389-
publisher = self.get_container(service_name).get_publisher(by_port=port)
398+
publisher = self.get_container(service_name).get_publisher(by_port=port).normalize()
390399
return publisher.URL, publisher.PublishedPort
391400

392401
@wait_container_is_ready(HTTPError, URLError)

0 commit comments

Comments
 (0)
Please sign in to comment.