Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions aiopslab/service/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@

import docker
import subprocess
import os
Copy link
Collaborator

Choose a reason for hiding this comment

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

can be removed?

import json


class Docker:
def __init__(self):
self.client = docker.from_env()
def __init__(self, base_url: str | None = None):
self.client = None
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

If Docker client initialization fails completely, self.client remains None, which will cause AttributeError when other methods try to use it. Consider raising an exception or implementing proper error handling in methods that use self.client.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please raise an exception if the client is not initialised.


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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

can be removed?


context_host = _docker_context_host()
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

The function _docker_context_host() is called without checking if docker command is available on the system, which could cause subprocess errors on systems without Docker CLI installed.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Valid point

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."""
Expand Down Expand Up @@ -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