docker Module¶
Docker and container management utilities.
Import¶
Functions¶
get_compose_command¶
Find the docker-compose or podman-compose command.
compose_cmd = docker.get_compose_command()
# Returns: "docker compose", "docker-compose", "podman-compose", or None
Checks for (in order):
1. podman-compose
2. docker compose (v2)
3. docker-compose (v1)
Returns: Command string or None if not found.
volume_exists¶
Check if a container volume exists.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
str | Volume name |
Returns: bool
create_volume¶
Create a container volume.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
str | Volume name |
Returns: ChangeList
container_exists¶
Check if a container exists (running or stopped).
Parameters:
| Name | Type | Description |
|---|---|---|
name |
str | Container name |
Returns: bool
is_container_up¶
Check if a container is running.
if docker.is_container_up("myapp"):
print("Container is running")
else:
print("Container is stopped or doesn't exist")
Parameters:
| Name | Type | Description |
|---|---|---|
name |
str | Container name |
Returns: bool
image_exists¶
Check if a container image exists locally.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
str | Image name |
Returns: bool
Configuration¶
The container command (docker or podman) is configured via settings:
Examples¶
Ensure Container Running¶
import fscm
from fscm.modules import docker
def ensure_container_running(name: str, image: str, ports: dict = None):
"""Ensure a container is running."""
if docker.is_container_up(name):
return # Already running
# Build run command
cmd = f"docker run -d --name {name}"
if ports:
for host, container in ports.items():
cmd += f" -p {host}:{container}"
cmd += f" {image}"
if docker.container_exists(name):
# Remove stopped container
fscm.run(f"docker rm {name}")
fscm.run(cmd)
# Usage
ensure_container_running("redis", "redis:alpine", {"6379": "6379"})
Docker Compose Workflow¶
import fscm
from fscm.modules import docker
def deploy_compose_stack(compose_dir: str):
"""Deploy a docker-compose stack."""
compose_cmd = docker.get_compose_command()
if not compose_cmd:
raise RuntimeError("Docker Compose not found")
# Pull latest images
fscm.run(f"{compose_cmd} pull", cwd=compose_dir)
# Start services
fscm.run(f"{compose_cmd} up -d", cwd=compose_dir)
# Show status
fscm.run(f"{compose_cmd} ps", cwd=compose_dir)
# Usage
deploy_compose_stack("/opt/mystack")
Volume Management¶
from fscm.modules import docker
def setup_persistent_volumes():
"""Create required volumes."""
volumes = ["postgres_data", "redis_data", "app_uploads"]
for vol in volumes:
if not docker.volume_exists(vol):
docker.create_volume(vol)
print(f"Created volume: {vol}")
else:
print(f"Volume exists: {vol}")
setup_persistent_volumes()
Health Check¶
import fscm
from fscm.modules import docker
def check_stack_health(containers: list[str]):
"""Check if all containers are running."""
all_healthy = True
for name in containers:
if docker.is_container_up(name):
print(f" {name}: UP")
else:
print(f" {name}: DOWN")
all_healthy = False
return all_healthy
# Usage
containers = ["nginx", "app", "postgres", "redis"]
if not check_stack_health(containers):
print("Some containers are not running!")