Skip to content

Commit

Permalink
fix(core): Fix retrieval of the docker socket path when using rootles…
Browse files Browse the repository at this point in the history
…s docker (required to run ryuk). Fixes testcontainers#537
  • Loading branch information
vemonet committed Oct 2, 2024
1 parent 85d6078 commit bba928f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 13 additions & 2 deletions core/testcontainers/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"))

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 2 additions & 7 deletions core/testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit bba928f

Please sign in to comment.