From 2aa3d371647877db45eac1663814dcc99de0f6af Mon Sep 17 00:00:00 2001 From: David Ankin Date: Sat, 25 May 2024 15:32:38 -0400 Subject: [PATCH] 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 --- core/testcontainers/compose/compose.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/testcontainers/compose/compose.py b/core/testcontainers/compose/compose.py index 5931b35a..951aee6d 100644 --- a/core/testcontainers/compose/compose.py +++ b/core/testcontainers/compose/compose.py @@ -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 @@ -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") @@ -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, @@ -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)