-
Notifications
You must be signed in to change notification settings - Fork 136
feat: fix api fetch error for docker client #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,32 @@ | |
|
|
||
| import docker | ||
| import subprocess | ||
| import os | ||
| import json | ||
|
|
||
|
|
||
| class Docker: | ||
| def __init__(self): | ||
| self.client = docker.from_env() | ||
| def __init__(self, base_url: str | None = None): | ||
| self.client = None | ||
|
||
|
|
||
| try: | ||
| env_client = docker.from_env() | ||
| env_client.ping() | ||
| self.client = env_client | ||
| return | ||
| except Exception: | ||
| print("Failed to connect to docker client from docker env") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a more descriptive error message including the base_url and the exception? |
||
| pass | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be removed? |
||
|
|
||
| context_host = _docker_context_host() | ||
|
||
| try: | ||
| if context_host: | ||
| env_client = docker.DockerClient(base_url=context_host) | ||
| env_client.ping() | ||
| self.client = env_client | ||
| return | ||
| except Exception as e: | ||
| print(f"Failed to connect to docker client from {context_host}: {e}") | ||
|
|
||
| def list_containers(self): | ||
| """Get all containers.""" | ||
|
|
@@ -55,3 +76,40 @@ def exec_command(self, command: str, input_data=None, cwd=None): | |
| return out.stdout.decode("utf-8") | ||
| except subprocess.CalledProcessError as e: | ||
| return e.stderr.decode("utf-8") | ||
|
|
||
|
|
||
| def _docker_context_host() -> str | None: | ||
| try: | ||
| ctx = subprocess.run( | ||
| ["docker", "context", "show"], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ).stdout.strip() | ||
| if not ctx: | ||
| return None | ||
| out = subprocess.run( | ||
| [ | ||
| "docker", | ||
| "context", | ||
| "inspect", | ||
| ctx, | ||
| "--format", | ||
| "{{json .Endpoints.docker.Host}}", | ||
| ], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ).stdout.strip() | ||
| if not out: | ||
| return None | ||
| # Output is a JSON string like "unix:///Users/.../.docker/run/docker.sock" | ||
| try: | ||
| host = json.loads(out) | ||
| return host | ||
| except json.JSONDecodeError: | ||
| return out.strip('"') | ||
| except Exception: | ||
| return None | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be removed?