Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DinD host detection when using local socket #283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions core/testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import docker
from docker.errors import NotFound
from docker.models.containers import Container, ContainerCollection
from docker.transport import UnixHTTPAdapter
import functools as ft
import os
from typing import List, Optional, Union
Expand Down Expand Up @@ -102,11 +103,14 @@ def host(self) -> str:

except ValueError:
return None
adapter = self.client.api.get_adapter(self.client.api.base_url)
is_ipc = isinstance(adapter, UnixHTTPAdapter)
is_ipc |= hasattr(adapter, "socket_path") or hasattr(adapter, "npipe_path")
is_ipc |= 'unix' in url.scheme or 'npipe' in url.scheme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the NpipeHTTPAdapter is only used on windows, would it still be available for an isinstance check independent of the platform? Or does the docker package not even expose NpipeHTTPAdapter if it's not on windows?

Alternatively, would checking adapter.__class__.name == 'NpipeHTTPAdapter' be easier to understand than checking the attributes of the adapter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that NpipeHTTPAdapter symbol is available outside of windows. Checking the class name would break for NpipeHTTPAdapter subclasses. So I went on with a classic duck-typing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like this:

try:
    from docker.transport.npipeconn import NpipeHTTPAdapter
except ImportError:
    class NpipeHTTPAdapter: ...

Would be a more proper solution.

It is much clearer what happens, does not depend the design of the Adapter and respects subclasses.

if is_ipc and inside_container():
ip_address = default_gateway_ip()
if ip_address:
return ip_address
if 'http' in url.scheme or 'tcp' in url.scheme:
return url.hostname
if 'unix' in url.scheme or 'npipe' in url.scheme:
if inside_container():
ip_address = default_gateway_ip()
if ip_address:
return ip_address
return "localhost"