FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix DinD host detection when using local socket by ikravets · Pull Request #283 · testcontainers/testcontainers-python · GitHub

fix DinD host detection when using local socket - #283

Open
ikravets wants to merge 1 commit into
testcontainers:mainfrom
ikravets:fix-dind-host-detection
Open

fix DinD host detection when using local socket#283
ikravets wants to merge 1 commit into
testcontainers:mainfrom
ikravets:fix-dind-host-detection

Conversation

Copy link
Copy Markdown
Contributor

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:

As NpipeHTTPAdapter type may not be available outside of Windows we rely on adapter-specific attributes.

codecov-commenter commented Dec 27, 2022
edited
Loading

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (main@928af5a). Click here to learn what that means.
The diff coverage is n/a.

❗ Current head afa057e differs from pull request most recent head f9d4508. Consider uploading reports for the commit f9d4508 to get more accurate results

@@           Coverage Diff           @@
##             main     #283   +/-   ##
=======================================
  Coverage        ?   87.30%           
=======================================
  Files           ?       30           
  Lines           ?      843           
  Branches        ?       57           
=======================================
  Hits            ?      736           
  Misses          ?       76           
  Partials        ?       31           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

ikravets force-pushed the fix-dind-host-detection branch from dfd26e6 to afa057e Compare January 1, 2023 08:18

ikravets commented Jan 1, 2023

Copy link
Copy Markdown
Contributor Author

Rebased to the master. @tillahoffmann, any chance for a review?

ikravets force-pushed the fix-dind-host-detection branch from afa057e to 01f9e77 Compare January 10, 2023 15:49

Copy link
Copy Markdown
Contributor Author

rebased to master

Copy link
Copy Markdown
Contributor

Thank you for these improvements! Are there tests that we could add that fail before the change is introduced to avoid future regressions?

tillahoffmann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is great, thank you for having such a detailed look into improving the host detection. Host detection is the challenge that seems to cause most trouble for our users so far.

Do you know whether this change would also resolve the issue of localnpipe appearing in the connection string, as discussed in #108? You're much more clued up on this than I am.

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

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.
ikravets force-pushed the fix-dind-host-detection branch from f6eea5f to f9d4508 Compare July 5, 2023 07:46

ikravets commented Jul 5, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

@tillahoffmann Hi, it's been more than half a year since I've opened this PR. Can we maybe merge this?

Copy link
Copy Markdown
Contributor Author

@tillahoffmann a kind ping

Copy link
Copy Markdown
Contributor

This should be obsolete now that #714 is merged.
Could you check?
If you think the detection is still required maybe add a is_socket_connection method or property and include the results of the check to either host or get_connection_mode of the DockerClient.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL