Skip to content

Commit bcfb770

Browse files
committed
fix running testcontainer inside a container
see testcontainers#475 (comment) for a summary
1 parent d8de3dd commit bcfb770

3 files changed

Lines changed: 77 additions & 29 deletions

File tree

‎core/testcontainers/core/config.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
from dataclasses import dataclass, field
2+
from enum import Enum
23
from logging import warning
34
from os import environ
45
from os.path import exists
56
from pathlib import Path
67
from typing import Optional, Union
78

9+
10+
class ConnectionMode(Enum):
11+
bridge_ip = "bridge_ip"
12+
gateway_ip = "gateway_ip"
13+
docker_host = "docker_host"
14+
15+
@property
16+
def use_mapped_port(self) -> bool:
17+
"""
18+
Return true if we need to use mapped port for this connection
19+
20+
This is true for everything but bridge mode.
21+
"""
22+
if self == self.bridge_ip:
23+
return False
24+
return True
25+
26+
827
MAX_TRIES = int(environ.get("TC_MAX_TRIES", 120))
928
SLEEP_TIME = int(environ.get("TC_POOLING_INTERVAL", 1))
1029
TIMEOUT = MAX_TRIES * SLEEP_TIME
@@ -20,6 +39,19 @@
2039
TC_GLOBAL = Path.home() / TC_FILE
2140

2241

42+
def get_user_overwritten_connection_mode() -> Optional[ConnectionMode]:
43+
"""
44+
Return the user overwritten connection mode.
45+
"""
46+
connection_mode: str | None = environ.get("TESTCONTAINERS_CONNECTION_MODE")
47+
if connection_mode:
48+
try:
49+
return ConnectionMode(connection_mode)
50+
except ValueError as e:
51+
raise ValueError(f"Error parsing TESTCONTAINERS_CONNECTION_MODE: {e}") from e
52+
return None
53+
54+
2355
def read_tc_properties() -> dict[str, str]:
2456
"""
2557
Read the .testcontainers.properties for settings. (see the Java implementation for details)
@@ -54,6 +86,8 @@ class TestcontainersConfiguration:
5486
tc_properties: dict[str, str] = field(default_factory=read_tc_properties)
5587
_docker_auth_config: Optional[str] = field(default_factory=lambda: environ.get("DOCKER_AUTH_CONFIG"))
5688
tc_host_override: Optional[str] = TC_HOST_OVERRIDE
89+
connection_mode_override: Optional[ConnectionMode] = None
90+
5791
"""
5892
https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644
5993
if os env TC_HOST is set, use it

‎core/testcontainers/core/container.py‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import docker.errors
66
from docker import version
77
from docker.types import EndpointConfig
8-
from typing_extensions import Self
8+
from typing_extensions import Self, assert_never
99

1010
from testcontainers.core.config import testcontainers_config as c
1111
from testcontainers.core.docker_client import DockerClient
1212
from testcontainers.core.exceptions import ContainerStartException
1313
from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
1414
from testcontainers.core.network import Network
15-
from testcontainers.core.utils import inside_container, is_arm, setup_logger
15+
from testcontainers.core.utils import is_arm, setup_logger
1616
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
1717

1818
if TYPE_CHECKING:
@@ -128,33 +128,23 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
128128
self.stop()
129129

130130
def get_container_host_ip(self) -> str:
131-
# infer from docker host
132-
host = self.get_docker_client().host()
133-
134-
# # check testcontainers itself runs inside docker container
135-
# if inside_container() and not os.getenv("DOCKER_HOST") and not host.startswith("http://"):
136-
# # If newly spawned container's gateway IP address from the docker
137-
# # "bridge" network is equal to detected host address, we should use
138-
# # container IP address, otherwise fall back to detected host
139-
# # address. Even it's inside container, we need to double check,
140-
# # because docker host might be set to docker:dind, usually in CI/CD environment
141-
# gateway_ip = self.get_docker_client().gateway_ip(self._container.id)
142-
143-
# if gateway_ip == host:
144-
# return self.get_docker_client().bridge_ip(self._container.id)
145-
# return gateway_ip
146-
return host
131+
connection_mode: c.ConnectionMode
132+
connection_mode = self.get_docker_client().get_connection_mode()
133+
if connection_mode == c.ConnectionMode.docker_host:
134+
return self.get_docker_client().host()
135+
elif connection_mode == c.ConnectionMode.gateway_ip:
136+
return self.get_docker_client().gateway_ip(self._container.id)
137+
elif connection_mode == c.ConnectionMode.bridge_ip:
138+
return self.get_docker_client().bridge_ip(self._container.id)
139+
else:
140+
# ensure that we covered all possible connection_modes
141+
assert_never(connection_mode)
147142

148143
@wait_container_is_ready()
149-
def get_exposed_port(self, port: int) -> str:
150-
mapped_port = self.get_docker_client().port(self._container.id, port)
151-
if inside_container():
152-
gateway_ip = self.get_docker_client().gateway_ip(self._container.id)
153-
host = self.get_docker_client().host()
154-
155-
if gateway_ip == host:
156-
return port
157-
return mapped_port
144+
def get_exposed_port(self, port: int) -> int:
145+
if self.get_docker_client().get_connection_mode().use_mapped_port:
146+
return self.get_docker_client().port(self._container.id, port)
147+
return port
158148

159149
def with_command(self, command: str) -> Self:
160150
self._command = command

‎core/testcontainers/core/docker_client.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import importlib.metadata
1515
import ipaddress
1616
import os
17+
import socket
1718
import urllib
1819
import urllib.parse
1920
from collections.abc import Iterable
@@ -128,7 +129,8 @@ def find_host_network(self) -> Optional[str]:
128129
# If we're docker in docker running on a custom network, we need to inherit the
129130
# network settings, so we can access the resulting container.
130131
try:
131-
docker_host = ipaddress.IPv4Address(self.host())
132+
host_ip = socket.gethostbyname(self.host())
133+
docker_host = ipaddress.IPv4Address(host_ip)
132134
# See if we can find the host on our networks
133135
for network in self.client.networks.list(filters={"type": "custom"}):
134136
if "IPAM" in network.attrs:
@@ -139,7 +141,7 @@ def find_host_network(self) -> Optional[str]:
139141
continue
140142
if docker_host in subnet:
141143
return network.name
142-
except ipaddress.AddressValueError:
144+
except (ipaddress.AddressValueError, OSError):
143145
pass
144146
return None
145147

@@ -187,6 +189,28 @@ def gateway_ip(self, container_id: str) -> str:
187189
network_name = self.network_name(container_id)
188190
return container["NetworkSettings"]["Networks"][network_name]["Gateway"]
189191

192+
def get_connection_mode(self) -> c.ConnectionMode:
193+
"""
194+
Determine the connection mode.
195+
196+
See https://github.com/testcontainers/testcontainers-python/issues/475#issuecomment-2407250970
197+
"""
198+
if c.connection_mode_override:
199+
return c.connection_mode_override
200+
localhosts = {"localhost", "127.0.0.1", "::1"}
201+
if not inside_container() or self.host() not in localhosts:
202+
# if running not inside a container or with a non-local docker client,
203+
# connect ot the docker host per default
204+
return c.ConnectionMode.docker_host
205+
elif self.find_host_network():
206+
# a host network could be determined, indicator for DooD,
207+
# so we should connect to the bridge_ip as the container we run in
208+
# and the one we started are connected to the same network
209+
# that might have no access to either docker_host or the gateway
210+
return c.ConnectionMode.bridge_ip
211+
# default for DinD
212+
return c.ConnectionMode.gateway_ip
213+
190214
def host(self) -> str:
191215
"""
192216
Get the hostname or ip address of the docker host.

0 commit comments

Comments
 (0)