stack_runtime
What it is
Utilities for interacting with a Docker Compose stack at runtime:
- Run
docker compose ...commands with Click-friendly error reporting - List services, inspect service/container states, and fetch/follow logs
Public API
-
@dataclass ComposeServiceState- Immutable container/service status snapshot with fields:
service: strcontainer_name: str | Nonestate: str(lowercased; defaults to"unknown")health: str | Noneexit_code: int | Nonestatus: str | None
- Immutable container/service status snapshot with fields:
-
run_compose(args: list[str], capture_output: bool = False) -> subprocess.CompletedProcess- Executes
docker compose <args>withcheck=True. - Raises
click.ClickExceptionwhen Docker is missing or the command fails.
- Executes
-
compose_service_list() -> list[str]- Returns service names from
docker compose config --services.
- Returns service names from
-
compose_service_states() -> dict[str, ComposeServiceState]- Returns a mapping
{service_name: ComposeServiceState}based ondocker compose ps -a --format json. - Handles both JSON arrays and newline-delimited JSON objects.
- Returns a mapping
-
compose_service_logs(service: str, tail: int = 120) -> str- Returns recent logs (
--tail) for a service viadocker compose logs --no-color.
- Returns recent logs (
-
compose_logs_follow(service: str | None) -> None- Streams logs (
docker compose logs -f) for all services or a specific service.
- Streams logs (
Configuration/Dependencies
- Requires Docker CLI with the
docker composesubcommand available inPATH. - Uses
click.ClickExceptionfor user-facing errors.
Usage
from naas_abi_cli.cli.stack_runtime import (
compose_service_list,
compose_service_states,
compose_service_logs,
)
print("Services:", compose_service_list())
states = compose_service_states()
for name, st in states.items():
print(name, st.state, st.exit_code)
print(compose_service_logs("web", tail=20))
Caveats
- Commands run in the current working directory; Compose project/stack selection is determined by the local Compose files and environment.
compose_logs_follow(...)does not capture output; it streams directly to the terminal.- Parsing of
docker compose ps --format jsonis best-effort; non-JSON lines are ignored.