Skip to content

fix: on windows, DockerCompose.get_service_host returns an unusable "0.0.0.0" - adjust to 127.0.0.1 #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -356,7 +365,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 @@ -378,14 +387,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