cleanup-docker.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Usage:
  4. # ./scripts/cleanup-docker.sh # safe cleanup (dangling images, stopped containers, build cache)
  5. # ./scripts/cleanup-docker.sh --all # also prune unused images
  6. # ./scripts/cleanup-docker.sh --volumes # also prune unused volumes (DANGEROUS)
  7. # ./scripts/cleanup-docker.sh --nuke-box # remove known box containers + named volumes (VERY DANGEROUS)
  8. ALL=0
  9. VOLUMES=0
  10. NUKE_BOX=0
  11. for arg in "${@:-}"; do
  12. case "$arg" in
  13. --all) ALL=1 ;;
  14. --volumes) VOLUMES=1 ;;
  15. --nuke-box) NUKE_BOX=1 ;;
  16. *) echo "Unknown arg: $arg" >&2; exit 2 ;;
  17. esac
  18. done
  19. echo "== docker cleanup starting =="
  20. echo "== pruning stopped containers =="
  21. docker container prune -f >/dev/null || true
  22. echo "== pruning build cache =="
  23. docker builder prune -f >/dev/null || true
  24. echo "== pruning dangling images =="
  25. docker image prune -f >/dev/null || true
  26. if [ "$ALL" -eq 1 ]; then
  27. echo "== pruning unused images (--all) =="
  28. docker image prune -af >/dev/null || true
  29. fi
  30. if [ "$VOLUMES" -eq 1 ]; then
  31. echo "== pruning unused volumes (--volumes) =="
  32. docker volume prune -f >/dev/null || true
  33. fi
  34. if [ "$NUKE_BOX" -eq 1 ]; then
  35. echo "== removing known box containers (--nuke-box) =="
  36. docker rm -f box-mgnt-api box-app-api box-stats-api box-mysql box-mongodb box-rabbitmq box-redis 2>/dev/null || true
  37. echo "== removing known box volumes (--nuke-box) =="
  38. docker volume rm box_mongo_data box_redis_data box_rabbitmq_data box_mysql_data 2>/dev/null || true
  39. echo "== removing box network (--nuke-box) =="
  40. docker network rm box-network 2>/dev/null || true
  41. fi
  42. echo "== docker cleanup done =="
  43. docker system df || true