Skip to content

fix: detect Docker host from context when DOCKER_HOST is not set#3684

Open
Itx-Psycho0 wants to merge 1 commit intoknative:mainfrom
Itx-Psycho0:fix/2226-docker-context-detection
Open

fix: detect Docker host from context when DOCKER_HOST is not set#3684
Itx-Psycho0 wants to merge 1 commit intoknative:mainfrom
Itx-Psycho0:fix/2226-docker-context-detection

Conversation

@Itx-Psycho0
Copy link
Copy Markdown
Contributor

Solution

Added Docker context detection by running docker context inspect to get the current context's Docker endpoint when DOCKER_HOST is not set. This allows func to automatically discover the correct socket path from Docker Desktop contexts.

Changes

  • Added getDockerContextHost() function in pkg/docker/docker_client.go that:
    -Runs docker context inspect command
    -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 socket

  • Added test case TestNewClient_DockerContext to verify the functionality

Detection Priority Order

1 DOCKER_HOST environment 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_HOST when using Docker Desktop. The tool will automatically detect the correct socket from the active Docker context.

Fixes #2226

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
@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 9, 2026 06:20
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 9, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Itx-Psycho0
Once this PR has been reviewed and has the lgtm label, please assign gauron99 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the size/L 🤖 PR changes 100-499 lines, ignoring generated files. label May 9, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 9, 2026

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@knative-prow knative-prow Bot added the needs-ok-to-test 🤖 Needs an org member to approve testing label May 9, 2026
@Itx-Psycho0
Copy link
Copy Markdown
Contributor Author

Hi maintainers,

I've implemented a fix for issue #2226 that adds Docker context detection when DOCKER_HOST is not set.

Summary:
Added automatic detection of Docker endpoint from the current Docker context
Fixes Docker Desktop compatibility on Mac and Linux
Maintains full backward compatibility
All tests pass

The solution runs docker context inspect to get the current context's endpoint, which allows func to work with Docker Desktop's context-specific sockets without requiring users to manually set DOCKER_HOST.

I'd appreciate your review when you have a chance.

@lkingland @dprotaso @matejvasek

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docker detection is broken on Mac OS X

1 participant