Skip to content

[DKP-2535] Honour context if present for default client, add contexts support#3401

Open
ebriney wants to merge 2 commits into
docker:mainfrom
ebriney:client-from-context
Open

[DKP-2535] Honour context if present for default client, add contexts support#3401
ebriney wants to merge 2 commits into
docker:mainfrom
ebriney:client-from-context

Conversation

@ebriney
Copy link
Copy Markdown
Member

@ebriney ebriney commented May 13, 2026

Summary of changes:

  • docker/context/api.py — new ContextAPI.kwargs_from_context(name=None, environment=None) helper that resolves a Docker CLI context (honouring DOCKER_CONTEXT, currentContext in ~/.docker/config.json, then default) and returns {base_url, tls} ready
    for APIClient.
  • docker/client.py —
    • DockerClient.from_env() now falls back to the current context when DOCKER_HOST is unset, so on a Docker Desktop machine docker.from_env() automatically talks to desktop-linux. Opt out with use_context=False.
    • New DockerClient.from_context(name=None, ...) classmethod for explicit context selection, also exported as docker.from_context.
  • docker/init.py — re-exports from_context.
  • tests/unit/client_test.py —
    • Pinned the two from_env pool-size tests with use_context=False so they stay deterministic regardless of the developer's current context.
    • New FromContextTest class covering: current-context fallback, DOCKER_HOST precedence, opt-out via use_context=False, from_context(name=...), unknown-context safety, and default-context resolution.

All 14 new/adjusted tests pass; the remaining pytest tests/unit/ failures are pre-existing on main (verified by stashing and re-running). The behavior matches the Docker CLI: DOCKER_HOST wins, then DOCKER_CONTEXT, then currentContext, then default.

ebriney and others added 2 commits May 13, 2026 10:50
Resolves a Docker CLI context (honouring DOCKER_CONTEXT, then
currentContext in ~/.docker/config.json, then the built-in default
context) and returns base_url / tls kwargs ready to be passed to
APIClient. Used by the client to talk to Docker Desktop out of the
box.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Emmanuel Briney <emmanuel.briney@docker.com>
DockerClient.from_env() now falls back to the current Docker CLI
context when DOCKER_HOST is unset, so docker.from_env() targets
Docker Desktop (or any other configured context) out of the box.
DOCKER_HOST still wins when set, matching the CLI's precedence.
Callers can opt out with use_context=False.

Also adds DockerClient.from_context(name=None) (exposed as
docker.from_context) for explicit context selection.

Existing pool-size unit tests are pinned with use_context=False so
they stay deterministic regardless of the developer's current
context. New tests cover: current-context fallback, DOCKER_HOST
precedence, opt-out, named-context selection, unknown-context
safety, and default-context resolution.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Signed-off-by: Emmanuel Briney <emmanuel.briney@docker.com>
@ebriney ebriney force-pushed the client-from-context branch from 39699ff to 37e5945 Compare May 13, 2026 08:51
@ebriney ebriney changed the title Honor context if present for default client [DKP-2535] Honour context if present for default client, add contexts support May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant