Skip to content

Raise ContextNotFound when from_context() gets an unknown name - #3470

Open
Beehtrix wants to merge 2 commits into
docker:mainfrom
Beehtrix:fix/from-context-unknown-name-3468
Open

Beehtrix wants to merge 2 commits into
docker:mainfrom
Beehtrix:fix/from-context-unknown-name-3468

Conversation

@Beehtrix

@Beehtrix Beehtrix commented Dec 4, 2026 •

Copy link
Copy Markdown

Fixes #3468

docker.from_context("typo") returned a client for the default local socket, because ContextAPI.kwargs_from_context() returns {} when the context is missing. A mistyped remote context name therefore sent calls to the local engine.

Changes:

  • ContextAPI.kwargs_from_context() takes a new strict=False argument. With strict=True it raises docker.errors.ContextNotFound(name), the same exception remove_context() and set_current_context() use.
  • DockerClient.from_context() passes strict=True only when name is given. from_context() with no name, and from_env() with an unknown DOCKER_CONTEXT, still fall back to the default socket as before, as the issue suggests.
  • An explicit empty name (from_context("")) also raises ContextNotFound instead of falling back.
  • Docstrings updated.

Tests (tests/unit/client_test.py): unknown name raises, no name with a bogus DOCKER_CONTEXT does not raise, strict mode raises, and an empty name raises. The existing test_from_context_uses_named_context now expects strict=True. On main the three new tests fail (3 failed in client_test.py); with the fix client_test.py plus context_test.py give 30 passed, 5 skipped.

Checks not run / caveats:

  • Integration tests (need a daemon) were not run.
  • Full tests/unit has 8 failures with and without this change (identical count on main). I did not investigate them.
  • ruff check over the whole repo reports 68 existing errors on main as well. Selected rules E,F,W,I on the three changed files pass.
  • No changelog entry: docs/change-log.md is written per release.
  • Commit is signed off (DCO).

from_context('typo') silently fell back to the default local socket,
so calls could reach the wrong engine. Add a strict flag to
ContextAPI.kwargs_from_context and use it in from_context() when a name
is passed explicitly. from_env() and from_context() without a name keep
their lenient DOCKER_CONTEXT behaviour.

Fixes docker#3468

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
get_context('') treats the empty string as no name and returns the current context, so from_context('') silently fell back. Raise ContextNotFound instead.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
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.

from_context() with an unknown name silently uses the local socket

1 participant