#!/bin/bash # Trading Bot v4 - Automated Docker Cleanup Script # Runs after builds to keep only last 2 images and prune dangling resources # Usage: ./cleanup_trading_bot.sh (or add to cron: 0 2 * * * /home/icke/traderv4/cleanup_trading_bot.sh) set -e echo "๐Ÿงน Trading Bot v4 - Docker Cleanup" echo "======================================" echo "" # Function to format bytes to human-readable format_size() { numfmt --to=iec-i --suffix=B "$1" 2>/dev/null || echo "$1 bytes" } # Check disk space before cleanup echo "๐Ÿ“Š Disk space BEFORE cleanup:" df -h / | tail -1 echo "" # Get current space usage BEFORE_IMAGES=$(docker system df --format "{{.Size}}" | head -1 | sed 's/[^0-9.]//g' | awk '{print $1}') BEFORE_BUILD_CACHE=$(docker system df --format "{{.Size}}" | sed -n 3p | sed 's/[^0-9.]//g' | awk '{print $1}') echo "๐Ÿ’พ Docker resources BEFORE cleanup:" docker system df echo "" # Keep only last 2 trading-bot images (for rollback safety) echo "๐Ÿ—‘๏ธ Step 1: Keeping last 2 trading-bot images, removing older ones..." IMAGES_TO_REMOVE=$(docker images traderv4-trading-bot --format "{{.ID}}" | tail -n +3) if [ -n "$IMAGES_TO_REMOVE" ]; then echo "$IMAGES_TO_REMOVE" | xargs docker rmi -f 2>/dev/null || true echo "โœ… Removed $(echo "$IMAGES_TO_REMOVE" | wc -l) old trading-bot image(s)" else echo "โœ… No old trading-bot images to remove (keeping last 2)" fi echo "" # Remove dangling images (untagged layers from builds) echo "๐Ÿ—‘๏ธ Step 2: Removing dangling images..." DANGLING=$(docker images -f "dangling=true" -q) if [ -n "$DANGLING" ]; then docker image prune -f echo "โœ… Removed dangling images" else echo "โœ… No dangling images found" fi echo "" # Prune build cache (biggest space saver - 40GB typical) echo "๐Ÿ—‘๏ธ Step 3: Pruning build cache..." docker builder prune -f echo "โœ… Build cache pruned" echo "" # Optional: Remove dangling volumes (be careful - only if no important data) echo "๐Ÿ—‘๏ธ Step 4: Checking for dangling volumes..." DANGLING_VOLUMES=$(docker volume ls -f "dangling=true" -q | grep -v "trading-bot-postgres" || true) if [ -n "$DANGLING_VOLUMES" ]; then echo "โš ๏ธ Found dangling volumes (excluding postgres):" echo "$DANGLING_VOLUMES" echo "โš ๏ธ Skipping volume cleanup for safety (run 'docker volume prune -f' manually if needed)" else echo "โœ… No dangling volumes found" fi echo "" # Show space saved echo "๐Ÿ“Š Disk space AFTER cleanup:" df -h / | tail -1 echo "" echo "๐Ÿ’พ Docker resources AFTER cleanup:" docker system df echo "" # Calculate space freed (approximate) AFTER_IMAGES=$(docker system df --format "{{.Size}}" | head -1 | sed 's/[^0-9.]//g' | awk '{print $1}') AFTER_BUILD_CACHE=$(docker system df --format "{{.Size}}" | sed -n 3p | sed 's/[^0-9.]//g' | awk '{print $1}') echo "โœ… Cleanup complete!" echo "" echo "๐Ÿ’ก Tips:" echo " - Run after each build: docker compose build && ./cleanup_trading_bot.sh" echo " - Add to cron: 0 2 * * * /home/icke/traderv4/cleanup_trading_bot.sh" echo " - BuildKit auto-cleanup kicks in at 20GB threshold (daemon.json)" echo "" echo "๐Ÿ”’ Safety measures:" echo " - Keeps last 2 trading-bot images for rollback" echo " - Never touches named volumes (postgres data safe)" echo " - Never removes running containers"