Skip to content
Open
Show file tree
Hide file tree
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
83 changes: 72 additions & 11 deletions lib/laddr/src/laddr/cli/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,96 @@
from pathlib import Path
import subprocess
import time
import platform
import os

import yaml

from .errors import DockerComposeError, DockerNotFoundError
from .logger import print_info, print_success


def check_docker() -> bool:
"""Check if Docker is installed and running.
def is_wsl():
"""Check if the environment is WSL."""
return "microsoft" in platform.uname().release.lower()

Returns:
True if Docker is available

Raises:
DockerNotFoundError: If Docker is not found or not running
"""
def _check_docker_daemon():
"""Check if the Docker daemon is running."""
try:
result = subprocess.run(
["docker", "version"],
capture_output=True,
text=True,
check=False,
timeout=5,
env={"DOCKER_HOST": "unix:///var/run/docker.sock"},
)
if result.returncode != 0:
raise DockerNotFoundError()
return True
return result.returncode == 0
except (FileNotFoundError, subprocess.TimeoutExpired):
raise DockerNotFoundError()
return False


def _check_docker_desktop():
"""Check if Docker Desktop is running."""
try:
result = subprocess.run(
["docker", "version"],
capture_output=True,
text=True,
check=False,
timeout=5,
)
return result.returncode == 0
except (FileNotFoundError, subprocess.TimeoutExpired):
return False


def check_docker() -> bool:
"""Check if Docker is installed and running.

Returns:
True if Docker is available

Raises:
DockerNotFoundError: If Docker is not found or not running
"""
system = platform.system()

if system == "Linux":
if is_wsl():
# Windows + WSL
if _check_docker_daemon():
return True
print_info("Docker daemon not found in WSL. Trying Docker Desktop...")
if _check_docker_desktop():
return True
raise DockerNotFoundError(
"Docker not found in WSL. Laddr could not connect to the Docker daemon within WSL or to Docker Desktop. Please ensure one of them is installed, running, and configured for WSL."
)
else:
# Linux
if _check_docker_daemon():
return True
print_info("Docker daemon not found. Trying Docker Desktop...")
if _check_docker_desktop():
return True
raise DockerNotFoundError(
"Docker not found. Laddr could not connect to the Docker daemon or Docker Desktop. Please ensure one of them is installed and running."
)
elif system == "Windows":
if _check_docker_desktop():
return True
raise DockerNotFoundError(
"Docker Desktop not found. Please ensure that Docker Desktop is installed and running."
)
else:
# Other systems
if _check_docker_desktop():
return True
raise DockerNotFoundError(
"Docker not found. Please ensure that Docker is installed and running."
)


def check_docker_compose() -> bool:
Expand Down
6 changes: 3 additions & 3 deletions lib/laddr/src/laddr/cli/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def __init__(self, filename: str, details: str):
class DockerNotFoundError(LaddrError):
"""Raised when Docker is not installed or not running."""

def __init__(self):
def __init__(self, message: str = "Docker not found or not running"):
super().__init__(
"Docker not found or not running",
hint="Install Docker (https://docs.docker.com/get-docker/) and ensure it's running",
message,
hint="Please ensure that Docker is installed and running.",
)


Expand Down