Skip to content

Honour CONTAINER_CONNECTION in PodmanClient.from_env() - #673

Closed
rjmcgee53192 wants to merge 2 commits into
containers:mainfrom
rjmcgee53192:contrib/fix-654
Closed

rjmcgee53192 wants to merge 2 commits into
containers:mainfrom
rjmcgee53192:contrib/fix-654

Conversation

@rjmcgee53192

@rjmcgee53192 rjmcgee53192 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Honour CONTAINER_CONNECTION in PodmanClient.from_env()

Fixes #654

Problem

from_env() read CONTAINER_HOST/DOCKER_HOST but ignored CONTAINER_CONNECTION,
which the podman CLI honours. The failure was silent: with CONTAINER_CONNECTION
pointing at a remote host, from_env() connected to the local socket and returned
entirely plausible data from the wrong machine.

Fix

When no explicit CONTAINER_HOST/DOCKER_HOST URL is set, from_env() now resolves
CONTAINER_CONNECTION through PodmanConfig.services — the same path as
PodmanClient(connection=...) — so the named connection's URL (and identity) is
applied. When both are set, CONTAINER_CONNECTION takes precedence, matching the
podman CLI (setupRemoteConnection checks CONTAINER_CONNECTION before
CONTAINER_HOST). An unknown connection name raises a clear ValueError instead of
silently falling back to the local socket. The docstring now documents
CONTAINER_CONNECTION.

Tests

Added 4 regression tests in podman/tests/unit/test_podmanclient.py:

  • test_from_env_container_connection: named connection resolves to its URL and identity
  • test_from_env_container_connection_takes_precedence: named connection wins over an explicit URL
  • test_from_env_unknown_connection: unknown name raises ValueError
  • test_from_env_no_connection_env: no env vars keeps the local-socket fallback unchanged

Results:

  • python3 -m unittest podman.tests.unit.test_podmanclient: 11/11 pass
  • Full unit suite: 255 tests pass, except a pre-existing test_utils import error
    (it shells out to a podman binary that isn't installed here; fails identically
    without this change)
  • ruff check + ruff format --check: clean
  • mypy podman/client.py: clean
  • pylint: 10.00/10
  • Coverage on podman/client.py: 98% (merge bar is 85%)

Notes

from_env() read CONTAINER_HOST/DOCKER_HOST but ignored CONTAINER_CONNECTION,
which the podman CLI honours. The failure was silent: it connected to the
local socket and returned plausible data from the wrong machine.

Now, when no explicit CONTAINER_HOST/DOCKER_HOST URL is set, from_env()
resolves CONTAINER_CONNECTION through PodmanConfig.services (the same path
as PodmanClient(connection=...)), and raises a clear ValueError for unknown
connection names instead of silently using the wrong host.

Fixes containers#654

Signed-off-by: rjmcgee53192 <[email protected]>
@MahediBuilds

Copy link
Copy Markdown
Contributor

Thanks for picking this up. Two things I noticed while testing this branch:

  1. Precedence is the reverse of the CLI. cmd/podman/root.go (setupRemoteConnection, priority comment at L225-236) checks CONTAINER_CONNECTION before CONTAINER_HOST. This PR lets CONTAINER_HOST win. With both set, podman-py and podman would talk to different hosts, which is the silent wrong-host situation from_env() ignores CONTAINER_CONNECTION and silently uses the local socket #654 describes. Making CONTAINER_CONNECTION win (and updating test_from_env_container_host_takes_precedence) would keep them aligned.
  2. It depends on fix: make service connection identity optional #656. On this branch alone, CONTAINER_CONNECTION=<a unix:// connection without identity> raises TypeError: expected str, bytes or os.PathLike object, not NoneType from ServiceConnection.identity. Merged together with fix: make service connection identity optional #656 (they merge cleanly), it resolves to the right socket. It might be worth noting that merge order in the description.

Smaller, possibly follow-up: the CLI also honours CONTAINER_SSHKEY next to CONTAINER_HOST.

Matches the podman CLI, which checks CONTAINER_CONNECTION before CONTAINER_HOST. An explicit CONTAINER_HOST URL no longer silently wins, so podman-py and the CLI always resolve to the same host.

Also notes the dependency on PR containers#656 for identity-less connections.

Signed-off-by: rjmcgee53192 <[email protected]>
@rjmcgee53192

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Both points addressed: CONTAINER_CONNECTION now takes precedence over CONTAINER_HOST to match the CLI, the precedence test is updated accordingly, and I've noted the #656 merge dependency in the PR description.

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code changes seem good. PPlease squashyour commits and properly sign them to satisfy DCO. Also check contribution guidelines and LLM policy.

@rjmcgee53192

Copy link
Copy Markdown
Contributor Author

Closing in favor of #682 — the same change squashed into a single commit with a proper Signed-off-by, per the review here. Thanks for the review, @Honny1; the new PR should show a green DCO.

This branch was previously deployed

1 inactive deployment
build — b0890cc0 Deployed Oct 20, 2026 by rjmcgee53192 via Test Build Python distribution 📦 #290
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_env() ignores CONTAINER_CONNECTION and silently uses the local socket

3 participants