Created
October 24, 2024 15:46
-
-
Save DaMandal0rian/9f30fae63cf2d55e22e8814eb6c7def8 to your computer and use it in GitHub Desktop.
Docker cleanup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| echo "🚀 Starting Docker cleanup script..." | |
| # Function to print colored output | |
| print_message() { | |
| local color=$1 | |
| local message=$2 | |
| case $color in | |
| "red") echo -e "\033[0;31m$message\033[0m" ;; | |
| "green") echo -e "\033[0;32m$message\033[0m" ;; | |
| "yellow") echo -e "\033[0;33m$message\033[0m" ;; | |
| "blue") echo -e "\033[0;34m$message\033[0m" ;; | |
| esac # Changed 'done' to 'esac' | |
| } | |
| # Function to display disk usage before and after cleanup | |
| show_disk_usage() { | |
| echo "💾 Disk usage:" | |
| df -h | grep /dev/ | |
| } | |
| # Show initial disk usage | |
| print_message "blue" "Initial system state:" | |
| show_disk_usage | |
| # Show current Docker disk usage | |
| print_message "blue" "\n🐳 Current Docker disk usage:" | |
| docker system df | |
| # Ask for confirmation | |
| read -p "⚠️ Are you sure you want to proceed with the cleanup? (y/n) " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]] | |
| then | |
| print_message "yellow" "Operation cancelled." | |
| exit 1 | |
| fi | |
| print_message "yellow" "\n🧹 Starting cleanup process..." | |
| # Remove all stopped containers | |
| print_message "blue" "\n📦 Removing stopped containers..." | |
| docker container prune -f | |
| # Remove all unused images | |
| print_message "blue" "\n🖼️ Removing unused images..." | |
| docker image prune -a -f | |
| # Remove all unused networks | |
| print_message "blue" "\n🌐 Removing unused networks..." | |
| docker network prune -f | |
| # Remove all volumes | |
| print_message "blue" "\n💾 Removing volumes..." | |
| echo "Current volumes:" | |
| docker volume ls | |
| print_message "yellow" "Removing all volumes..." | |
| docker volume ls -q | xargs -r docker volume rm | |
| # Final system prune | |
| print_message "blue" "\n🧹 Performing final system prune..." | |
| docker system prune -f | |
| # Show final disk usage | |
| print_message "green" "\n✅ Cleanup completed!" | |
| print_message "blue" "\nFinal system state:" | |
| show_disk_usage | |
| print_message "blue" "\n🐳 Final Docker disk usage:" | |
| docker system df | |
| print_message "green" "\n🎉 Docker cleanup completed successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment