Docker Management¶
Управление Docker контейнерами через API.
Установка¶
API¶
Получение списка контейнеров¶
from viai_kit.admin.docker import get_containers
containers = get_containers()
for c in containers:
print(f"{c['name']}: {c['state']}")
Управление контейнером¶
from viai_kit.admin.docker import (
start_container,
stop_container,
restart_container,
get_container_logs,
get_container_stats
)
# Запуск
start_container("my-container")
# Остановка
stop_container("my-container")
# Перезапуск
restart_container("my-container")
# Логи (последние 100 строк)
logs = get_container_logs("my-container", tail=100)
for log in logs:
print(f"[{log['stream']}] {log['message']}")
# Статистика
stats = get_container_stats("my-container")
print(f"CPU: {stats['cpu_percent']:.1f}%")
print(f"RAM: {stats['memory_percent']:.1f}%")
Структуры данных¶
Container¶
class Container(TypedDict):
id: str
name: str
image: str
status: str
state: Literal['running', 'exited', 'paused', 'restarting', 'dead']
created: str
ports: dict[str, list[PortBinding] | None]
networks: list[str]
ContainerStats¶
class ContainerStats(TypedDict):
cpu_percent: float
memory_percent: float
memory_usage: int
memory_limit: int
ContainerLog¶
FastAPI интеграция¶
from fastapi import FastAPI, APIRouter
from viai_kit.admin.docker import (
get_containers,
start_container,
stop_container,
get_container_logs
)
router = APIRouter(prefix="/docker")
@router.get("/containers")
def list_containers():
return get_containers()
@router.post("/containers/{name}/start")
def start(name: str):
start_container(name)
return {"status": "started"}
@router.post("/containers/{name}/stop")
def stop(name: str):
stop_container(name)
return {"status": "stopped"}
@router.get("/containers/{name}/logs")
def logs(name: str, tail: int = 100):
return get_container_logs(name, tail=tail)
Зависимости¶
docker>=7.0.0— Docker SDK для Python