From bba928fe180baa9dcdfb2e237f414ac910689cdb Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Wed, 2 Oct 2024 19:45:34 +0200 Subject: [PATCH] fix(core): Fix retrieval of the docker socket path when using rootless docker (required to run ryuk). Fixes https://github.com/testcontainers/testcontainers-python/issues/537 --- core/testcontainers/core/config.py | 15 +++++++++++++-- core/testcontainers/core/docker_client.py | 9 ++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/core/testcontainers/core/config.py b/core/testcontainers/core/config.py index 142dbc19..298c64e5 100644 --- a/core/testcontainers/core/config.py +++ b/core/testcontainers/core/config.py @@ -4,6 +4,7 @@ from os.path import exists from pathlib import Path from typing import Optional, Union +from urllib.parse import urlparse MAX_TRIES = int(environ.get("TC_MAX_TRIES", 120)) SLEEP_TIME = int(environ.get("TC_POOLING_INTERVAL", 1)) @@ -12,7 +13,7 @@ RYUK_IMAGE: str = environ.get("RYUK_CONTAINER_IMAGE", "testcontainers/ryuk:0.8.1") RYUK_PRIVILEGED: bool = environ.get("TESTCONTAINERS_RYUK_PRIVILEGED", "false") == "true" RYUK_DISABLED: bool = environ.get("TESTCONTAINERS_RYUK_DISABLED", "false") == "true" -RYUK_DOCKER_SOCKET: str = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/run/docker.sock") +RYUK_DOCKER_SOCKET: Optional[str] = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", None) RYUK_RECONNECTION_TIMEOUT: str = environ.get("RYUK_RECONNECTION_TIMEOUT", "10s") TC_HOST_OVERRIDE: Optional[str] = environ.get("TC_HOST", environ.get("TESTCONTAINERS_HOST_OVERRIDE")) @@ -49,7 +50,6 @@ class TestcontainersConfiguration: ryuk_image: str = RYUK_IMAGE ryuk_privileged: bool = RYUK_PRIVILEGED ryuk_disabled: bool = RYUK_DISABLED - ryuk_docker_socket: str = RYUK_DOCKER_SOCKET ryuk_reconnection_timeout: str = RYUK_RECONNECTION_TIMEOUT tc_properties: dict[str, str] = field(default_factory=read_tc_properties) _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]: def timeout(self) -> int: return self.max_tries * self.sleep_time + @property + def docker_host(self) -> Optional[str]: + return self.tc_properties_get_tc_host() or environ.get("DOCKER_HOST") + + @property + def ryuk_docker_socket(self) -> str: + if RYUK_DOCKER_SOCKET: + return RYUK_DOCKER_SOCKET + if self.docker_host: + return urlparse(self.docker_host).path + return "/var/run/docker.sock" testcontainers_config = TestcontainersConfiguration() diff --git a/core/testcontainers/core/docker_client.py b/core/testcontainers/core/docker_client.py index c540b9f7..8212f162 100644 --- a/core/testcontainers/core/docker_client.py +++ b/core/testcontainers/core/docker_client.py @@ -57,7 +57,7 @@ class DockerClient: """ def __init__(self, **kwargs) -> None: - docker_host = get_docker_host() + docker_host = c.docker_host if docker_host: LOGGER.info(f"using host {docker_host}") @@ -89,7 +89,7 @@ def run( **kwargs, ) -> Container: # If the user has specified a network, we'll assume the user knows best - if "network" not in kwargs and not get_docker_host(): + if "network" not in kwargs and not c.docker_host: # Otherwise we'll try to find the docker host for dind usage. host_network = self.find_host_network() if host_network: @@ -218,10 +218,5 @@ def client_networks_create(self, name: str, param: dict): labels = create_labels("", param.get("labels")) return self.client.networks.create(name, **{**param, "labels": labels}) - -def get_docker_host() -> Optional[str]: - return c.tc_properties_get_tc_host() or os.getenv("DOCKER_HOST") - - def get_docker_auth_config() -> Optional[str]: return c.docker_auth_config