fix: detect Docker host from context when DOCKER_HOST is not set#3684
fix: detect Docker host from context when DOCKER_HOST is not set#3684Itx-Psycho0 wants to merge 1 commit intoknative:mainfrom
Conversation
This commit fixes issue knative#2226 where Docker detection fails on Mac OS X and Linux when using Docker Desktop with context-specific sockets. Problem: When DOCKER_HOST environment variable is not set, func only checks the default socket path (/var/run/docker.sock). Docker Desktop uses context-specific sockets like: - Mac: ~/.docker/run/docker.sock (desktop-linux context) - Linux: ~/.docker/desktop/docker.sock (desktop-linux context) Solution: Added getDockerContextHost() function that runs 'docker context inspect' to get the current context's Docker endpoint. This is checked before falling back to the default socket path. Changes: - Added Docker context detection in NewClient() function - Created getDockerContextHost() helper function that parses docker context inspect output - Added test case to verify context detection works - Maintains backward compatibility with existing behavior The fix follows this priority order: 1. DOCKER_HOST environment variable (if set) 2. Current Docker context endpoint (new) 3. Default socket path 4. Podman detection (existing fallback) Fixes knative#2226
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Itx-Psycho0 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @Itx-Psycho0. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
Hi maintainers, I've implemented a fix for issue #2226 that adds Docker context detection when Summary: The solution runs I'd appreciate your review when you have a chance. @lkingland @dprotaso @matejvasek Thanks! |
Solution
Added Docker context detection by running
docker context inspectto get the current context's Docker endpoint whenDOCKER_HOSTis not set. This allowsfuncto automatically discover the correct socket path from Docker Desktop contexts.Changes
Added
getDockerContextHost()function inpkg/docker/docker_client.gothat:-Runs
docker context inspectcommand-Parses JSON output to extract the Docker endpoint
-Returns the endpoint or an empty string if unavailable
Updated
NewClient()to check Docker context before falling back to default socketAdded test case
TestNewClient_DockerContextto verify the functionalityDetection Priority Order
1
DOCKER_HOSTenvironment variable (if set)2 Current Docker context endpoint (new)
3 Default socket path
4 Podman detection (existing fallback)
Testing
✅ All unit tests pass
✅ Linting checks pass
✅ Backward compatible with existing behavior
✅ Works with Docker Desktop on Mac and Linux
Usage
Users no longer need to manually set
DOCKER_HOSTwhen using Docker Desktop. The tool will automatically detect the correct socket from the active Docker context.Fixes #2226