#!/usr/bin/env bash
#
# docker-cleanup.sh — Remove stopped containers, dangling images, and unused networks.
#
# Safe to run via systemd timer (daily). Uses Docker's built-in pruning
# commands with conservative scope.
#
# Does NOT prune volumes. Named volumes may hold durable data and cannot be
# safely auto-pruned in a deployment that mixes stateful workloads. When the
# Orb lane lands with labeled resources, volume pruning can be scoped to
# Orb-managed labels (e.g. --filter label=org.openputer.orb). Until then,
# manage volumes manually.

set -euo pipefail

echo "[docker-cleanup] $(date -u +%Y-%m-%dT%H:%M:%SZ)"

# Remove stopped containers older than 24 hours
echo "[docker-cleanup] Pruning stopped containers (>24h old)..."
docker container prune -f --filter "until=24h"

# Remove dangling images (untagged intermediate layers only)
echo "[docker-cleanup] Pruning dangling images..."
docker image prune -f

# Remove unused networks
echo "[docker-cleanup] Pruning unused networks..."
docker network prune -f

# Volumes are intentionally NOT pruned. See header comment.

# Show remaining disk usage
echo "[docker-cleanup] Docker disk usage:"
docker system df

echo "[docker-cleanup] Done."
