Skip to content

work around docker sdk weirdness#1198

Merged
evgunter merged 11 commits into
mainfrom
mngr/docker-weirdness
Apr 9, 2026
Merged

work around docker sdk weirdness#1198
evgunter merged 11 commits into
mainfrom
mngr/docker-weirdness

Conversation

@evgunter

@evgunter evgunter commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #1167

  • mngr list was failing with "Cannot list Docker hosts (Docker daemon unavailable?)" on macOS Docker Desktop even though docker ps worked fine.
  • Root cause: the Python Docker SDK's docker.from_env() does not read Docker contexts. It only checks DOCKER_HOST env var and falls back to /var/run/docker.sock. On macOS Docker Desktop, the active context is typically desktop-linux pointing to ~/.docker/run/docker.sock, which is what the Docker CLI uses -- but the SDK ignores this entirely.
  • This is a known upstream issue: docker/docker-py#3146 (open since June 2023). There's a WIP PR to fix it but it hasn't been merged, and v7.1.0 is still the latest SDK release.

What changed

  • Added _get_docker_context_host() which reads the active Docker context via docker.context.ContextAPI (already available in the SDK, just not used by from_env()).
  • Added create_docker_client() as the public entry point with the correct resolution order: explicit config > DOCKER_HOST env > Docker context > platform default.
  • Updated all docker.from_env() callsites (production and test infrastructure) to use create_docker_client().
  • Added deterministic tests using fake Docker configs via the DOCKER_CONFIG env var.

Docker SDK details

The SDK's Context._load_meta() raises bare Exception (not a subclass) for corrupted context metadata, which required a broad except Exception with a ratchet bump. The relevant SDK code:

except (OSError, KeyError, ValueError) as e:
    raise Exception(
        f"Detected corrupted meta file for context {name} : {e}"
    ) from e

Test plan

  • All unit/integration tests pass (3260 passed)
  • Deterministic tests for context resolution: non-default context, default context, missing config, malformed config, corrupted metadata
  • Verified create_docker_client() resolves to the correct socket on a macOS Docker Desktop machine

Generated with Claude Code

evgunter and others added 5 commits April 6, 2026 17:16
docker.from_env() does not consult Docker contexts -- it only reads
DOCKER_HOST and falls back to /var/run/docker.sock. On macOS with Docker
Desktop, the CLI works via the "desktop-linux" context pointing to
~/.docker/run/docker.sock, but the Python SDK fails if /var/run/docker.sock
is missing or broken. Now _docker_client reads the active Docker context
first (when DOCKER_HOST is not set), matching Docker CLI behavior.

Closes #1167

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…nistic tests

Extract the context-aware Docker client creation into a public
create_docker_client() function and use it in conftest.py and testing.py
(previously using docker.from_env() directly). Replace the
environment-dependent test with deterministic tests using a fake Docker
config directory via DOCKER_CONFIG env var.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Problem: The except clause caught (OSError, json.JSONDecodeError) but the
Docker SDK's Context._load_meta() raises a bare Exception when context
metadata is corrupted. A corrupted non-default context meta file would
propagate an unhandled exception instead of gracefully falling back to
docker.from_env().

Fix: Catch Exception broadly since the Docker SDK uses bare Exception
for corrupted context metadata. Added a test for this case and bumped
the broad-exception-catch ratchet count from 1 to 2.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Problem: write_fake_docker_context is a non-fixture helper function
placed in conftest.py and explicitly imported in test files. Per project
conventions, conftest.py should only contain fixtures and hooks.

Fix: Moved the helper to testing.py (where non-fixture test utilities
belong) and updated the import in instance_test.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@evgunter evgunter marked this pull request as ready for review April 7, 2026 19:44
@evgunter evgunter changed the title Fix Docker daemon connection on macOS Docker Desktop work around docker sdk weirdness Apr 7, 2026

def test_prevent_broad_exception_catch() -> None:
rc.check_broad_exception_catch(_DIR, snapshot(1))
rc.check_broad_exception_catch(_DIR, snapshot(2))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

docker sdk raises bare Exception -_-

The docs regeneration hook produced incorrect output because the vultr
plugin is not installed in this worktree.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@evgunter evgunter merged commit 4af1abb into main Apr 9, 2026
36 checks passed
@evgunter evgunter deleted the mngr/docker-weirdness branch April 9, 2026 21:11
@joshalbrecht

Copy link
Copy Markdown
Contributor

gross, but sure, LGTM

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.

Docker daemon error

2 participants