Skip to content

Commit bba928f

Browse files
committed
fix(core): Fix retrieval of the docker socket path when using rootless docker (required to run ryuk). Fixes testcontainers#537
1 parent 85d6078 commit bba928f

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎core/testcontainers/core/config.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from os.path import exists
55
from pathlib import Path
66
from typing import Optional, Union
7+
from urllib.parse import urlparse
78

89
MAX_TRIES = int(environ.get("TC_MAX_TRIES", 120))
910
SLEEP_TIME = int(environ.get("TC_POOLING_INTERVAL", 1))
@@ -12,7 +13,7 @@
1213
RYUK_IMAGE: str = environ.get("RYUK_CONTAINER_IMAGE", "testcontainers/ryuk:0.8.1")
1314
RYUK_PRIVILEGED: bool = environ.get("TESTCONTAINERS_RYUK_PRIVILEGED", "false") == "true"
1415
RYUK_DISABLED: bool = environ.get("TESTCONTAINERS_RYUK_DISABLED", "false") == "true"
15-
RYUK_DOCKER_SOCKET: str = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/run/docker.sock")
16+
RYUK_DOCKER_SOCKET: Optional[str] = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", None)
1617
RYUK_RECONNECTION_TIMEOUT: str = environ.get("RYUK_RECONNECTION_TIMEOUT", "10s")
1718
TC_HOST_OVERRIDE: Optional[str] = environ.get("TC_HOST", environ.get("TESTCONTAINERS_HOST_OVERRIDE"))
1819

@@ -49,7 +50,6 @@ class TestcontainersConfiguration:
4950
ryuk_image: str = RYUK_IMAGE
5051
ryuk_privileged: bool = RYUK_PRIVILEGED
5152
ryuk_disabled: bool = RYUK_DISABLED
52-
ryuk_docker_socket: str = RYUK_DOCKER_SOCKET
5353
ryuk_reconnection_timeout: str = RYUK_RECONNECTION_TIMEOUT
5454
tc_properties: dict[str, str] = field(default_factory=read_tc_properties)
5555
_docker_auth_config: Optional[str] = field(default_factory=lambda: environ.get("DOCKER_AUTH_CONFIG"))
@@ -79,6 +79,17 @@ def tc_properties_get_tc_host(self) -> Union[str, None]:
7979
def timeout(self) -> int:
8080
return self.max_tries * self.sleep_time
8181

82+
@property
83+
def docker_host(self) -> Optional[str]:
84+
return self.tc_properties_get_tc_host() or environ.get("DOCKER_HOST")
85+
86+
@property
87+
def ryuk_docker_socket(self) -> str:
88+
if RYUK_DOCKER_SOCKET:
89+
return RYUK_DOCKER_SOCKET
90+
if self.docker_host:
91+
return urlparse(self.docker_host).path
92+
return "/var/run/docker.sock"
8293

8394
testcontainers_config = TestcontainersConfiguration()
8495

‎core/testcontainers/core/docker_client.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class DockerClient:
5757
"""
5858

5959
def __init__(self, **kwargs) -> None:
60-
docker_host = get_docker_host()
60+
docker_host = c.docker_host
6161

6262
if docker_host:
6363
LOGGER.info(f"using host {docker_host}")
@@ -89,7 +89,7 @@ def run(
8989
**kwargs,
9090
) -> Container:
9191
# If the user has specified a network, we'll assume the user knows best
92-
if "network" not in kwargs and not get_docker_host():
92+
if "network" not in kwargs and not c.docker_host:
9393
# Otherwise we'll try to find the docker host for dind usage.
9494
host_network = self.find_host_network()
9595
if host_network:
@@ -218,10 +218,5 @@ def client_networks_create(self, name: str, param: dict):
218218
labels = create_labels("", param.get("labels"))
219219
return self.client.networks.create(name, **{**param, "labels": labels})
220220

221-
222-
def get_docker_host() -> Optional[str]:
223-
return c.tc_properties_get_tc_host() or os.getenv("DOCKER_HOST")
224-
225-
226221
def get_docker_auth_config() -> Optional[str]:
227222
return c.docker_auth_config

0 commit comments

Comments
 (0)