Skip to content

Commit f9d4508

Browse files
committed
fix DinD host detection when using local socket
When using IPC socket (unix/npipe) to communicate with local docker server docker-py implementation rewrites the base url to HTTP, see https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/api/client.py#L143-L168 We cannot rely on URL scheme in order to detect such connections. Instead, we detect connection adapters added by docker-py api client: - UnixHTTPAdapter https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/transport/unixconn.py - NpipeHTTPAdapter https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/transport/npipeconn.py As NpipeHTTPAdapter type may not be available outside of Windows we rely on adapter-specific attributes.
1 parent 928af5a commit f9d4508

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

core/testcontainers/core/docker_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import docker
1515
from docker.errors import NotFound
1616
from docker.models.containers import Container, ContainerCollection
17+
from docker.transport import UnixHTTPAdapter
1718
import functools as ft
1819
import os
1920
from typing import List, Optional, Union
@@ -102,11 +103,14 @@ def host(self) -> str:
102103

103104
except ValueError:
104105
return None
106+
adapter = self.client.api.get_adapter(self.client.api.base_url)
107+
is_ipc = isinstance(adapter, UnixHTTPAdapter)
108+
is_ipc |= hasattr(adapter, "socket_path") or hasattr(adapter, "npipe_path")
109+
is_ipc |= 'unix' in url.scheme or 'npipe' in url.scheme
110+
if is_ipc and inside_container():
111+
ip_address = default_gateway_ip()
112+
if ip_address:
113+
return ip_address
105114
if 'http' in url.scheme or 'tcp' in url.scheme:
106115
return url.hostname
107-
if 'unix' in url.scheme or 'npipe' in url.scheme:
108-
if inside_container():
109-
ip_address = default_gateway_ip()
110-
if ip_address:
111-
return ip_address
112116
return "localhost"

0 commit comments

Comments
 (0)