Skip to content

Commit dfd26e6

Browse files
ikravetsIlia Kravets
authored and
Ilia Kravets
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 2f67234 commit dfd26e6

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

testcontainers/core/docker_client.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import urllib
1515
import docker
1616
from docker.models.containers import Container
17+
from docker.transport import UnixHTTPAdapter
1718
from testcontainers.core.utils import inside_container
1819
from testcontainers.core.utils import default_gateway_ip
1920

@@ -72,11 +73,14 @@ def host(self):
7273

7374
except ValueError:
7475
return None
76+
adapter = self.client.api.get_adapter(self.client.api.base_url)
77+
is_ipc = isinstance(adapter, UnixHTTPAdapter)
78+
is_ipc |= hasattr(adapter, "socket_path") or hasattr(adapter, "npipe_path")
79+
is_ipc |= 'unix' in url.scheme or 'npipe' in url.scheme
80+
if is_ipc and inside_container():
81+
ip_address = default_gateway_ip()
82+
if ip_address:
83+
return ip_address
7584
if 'http' in url.scheme or 'tcp' in url.scheme:
7685
return url.hostname
77-
if 'unix' in url.scheme or 'npipe' in url.scheme:
78-
if inside_container():
79-
ip_address = default_gateway_ip()
80-
if ip_address:
81-
return ip_address
8286
return "localhost"

0 commit comments

Comments
 (0)