#!/bin/bash
# Safe Docker cleanup script after development
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log() {
local color=$1
shift
echo -e "${color}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $*"
}
# Show current usage
show_usage() {
log $BLUE "=== Current Docker Disk Usage ==="
docker system df
echo ""
}
# Safety check - ensure our containers are running
check_running_containers() {
log $BLUE "=== Checking Running Containers ==="
if docker ps | grep -q "mcp-comfyui"; then
log $GREEN "✓ MCP-ComfyUI containers are running"
docker ps --format "table {{.Names}}\t{{.Status}}" | grep -E "(NAME|mcp-comfyui)"
else
log $YELLOW "⚠ MCP-ComfyUI containers are not running"
log $YELLOW " Run 'docker-compose -p mcp-comfyui up -d' to start them"
fi
echo ""
}
# Level 1: Safe cleanup (dangling only)
cleanup_safe() {
log $GREEN "=== Level 1: Safe Cleanup (Dangling Resources) ==="
log $BLUE "Removing dangling images..."
docker image prune -f
log $BLUE "Removing stopped containers..."
docker container prune -f
log $BLUE "Removing unused networks..."
docker network prune -f
echo ""
}
# Level 2: Moderate cleanup (unused images, keep tagged)
cleanup_moderate() {
log $YELLOW "=== Level 2: Moderate Cleanup (Unused Images) ==="
log $BLUE "Removing unused images (keeping tagged images)..."
docker image prune -a -f --filter "until=24h"
log $BLUE "Removing anonymous volumes..."
docker volume prune -f
echo ""
}
# Level 3: Build cache cleanup
cleanup_buildcache() {
log $YELLOW "=== Level 3: Build Cache Cleanup ==="
# Keep recent cache (last 7 days)
log $BLUE "Removing old build cache (keeping last 7 days)..."
docker builder prune -f --keep-storage=10GB --filter "until=168h"
echo ""
}
# Level 4: Aggressive cleanup (use with caution)
cleanup_aggressive() {
log $RED "=== Level 4: Aggressive Cleanup ==="
log $RED "⚠ WARNING: This will remove ALL unused images and volumes!"
read -p "Are you sure? This will remove all unused Docker resources (y/N): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
log $BLUE "Removing all unused images..."
docker image prune -a -f
log $BLUE "Removing all unused volumes..."
docker volume prune -f
log $BLUE "Removing all build cache..."
docker builder prune -a -f
else
log $YELLOW "Skipped aggressive cleanup"
fi
echo ""
}
# Full system cleanup (Docker's built-in)
cleanup_system() {
log $RED "=== System-wide Docker Cleanup ==="
log $RED "⚠ WARNING: This will remove all stopped containers, unused networks, dangling images, and build cache!"
read -p "Run docker system prune? (y/N): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
docker system prune -a -f --volumes
else
log $YELLOW "Skipped system cleanup"
fi
echo ""
}
# Show space saved
show_saved() {
log $GREEN "=== Cleanup Complete ==="
log $BLUE "New disk usage:"
docker system df
}
# Main menu
main() {
show_usage
check_running_containers
log $GREEN "=== Docker Cleanup Options ==="
echo "1) Safe cleanup (dangling images, stopped containers)"
echo "2) Moderate cleanup (+ unused images older than 24h)"
echo "3) Build cache cleanup (keep 10GB of recent cache)"
echo "4) Aggressive cleanup (ALL unused resources)"
echo "5) Full system prune (Docker's built-in)"
echo "6) Run all safe cleanups (1+2+3)"
echo "0) Exit"
echo ""
read -p "Select cleanup level (0-6): " choice
case $choice in
1)
cleanup_safe
;;
2)
cleanup_safe
cleanup_moderate
;;
3)
cleanup_buildcache
;;
4)
cleanup_aggressive
;;
5)
cleanup_system
;;
6)
cleanup_safe
cleanup_moderate
cleanup_buildcache
;;
0)
log $BLUE "Exiting without cleanup"
exit 0
;;
*)
log $RED "Invalid choice"
exit 1
;;
esac
show_saved
}
# Handle command line arguments
if [[ $# -gt 0 ]]; then
case $1 in
--safe)
show_usage
cleanup_safe
show_saved
;;
--moderate)
show_usage
cleanup_safe
cleanup_moderate
show_saved
;;
--aggressive)
show_usage
cleanup_aggressive
show_saved
;;
--all)
show_usage
cleanup_safe
cleanup_moderate
cleanup_buildcache
show_saved
;;
--help)
echo "Usage: $0 [OPTION]"
echo ""
echo "Docker cleanup utility with safety levels"
echo ""
echo "Options:"
echo " --safe Run safe cleanup only"
echo " --moderate Run safe + moderate cleanup"
echo " --aggressive Run aggressive cleanup (removes all unused)"
echo " --all Run all safe cleanups (1+2+3)"
echo " --help Show this help"
echo ""
echo "Without options, shows interactive menu"
;;
*)
log $RED "Unknown option: $1"
echo "Use --help for usage"
exit 1
;;
esac
else
# Interactive mode
main
fi