Старт

This commit is contained in:
2026-03-13 14:39:43 +08:00
commit a2cc480644
88 changed files with 18526 additions and 0 deletions

196
docker-cleanup.sh Normal file
View File

@@ -0,0 +1,196 @@
#!/bin/bash
# Docker Cleanup Script for Auth Learning App
# Usage: ./docker-cleanup.sh [--soft|--medium|--all]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE} Docker Cleanup Script${NC}"
echo -e "${BLUE}========================================${NC}"
}
print_step() {
echo -e "\n${YELLOW}[STEP]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_usage() {
echo "Usage: $0 [OPTION]"
echo "Options:"
echo " --soft Remove containers and networks (safe)"
echo " --medium Remove containers, networks, and dangling images (recommended)"
echo " --all Remove everything including volumes and build cache (DANGER: deletes database!)"
echo " --help Show this help message"
echo ""
echo "Default: --medium"
}
soft_cleanup() {
print_step "Performing SOFT cleanup (containers and networks)..."
# Stop and remove containers
if docker-compose ps -q > /dev/null 2>&1; then
print_step "Stopping and removing containers..."
docker-compose down
print_success "Containers stopped and removed"
else
print_warning "No running containers found for this project"
fi
# Remove project networks
print_step "Removing project networks..."
local networks=$(docker network ls --filter name=auth_learning -q)
if [ -n "$networks" ]; then
echo "$networks" | xargs -r docker network rm
print_success "Project networks removed"
else
print_warning "No project networks found"
fi
}
medium_cleanup() {
soft_cleanup
print_step "Performing MEDIUM cleanup (dangling images)..."
# Remove dangling images
print_step "Removing dangling images..."
local dangling_images=$(docker images -f "dangling=true" -q)
if [ -n "$dangling_images" ]; then
echo "$dangling_images" | xargs -r docker rmi
print_success "Dangling images removed"
else
print_warning "No dangling images found"
fi
# Remove unused images (without tags)
print_step "Removing unused images..."
local untagged_images=$(docker images --filter "dangling=false" --format "{{.Repository}}:{{.Tag}}" | grep "<none>" | cut -d' ' -f1)
if [ -n "$untagged_images" ]; then
echo "$untagged_images" | xargs -r docker rmi
print_success "Untagged images removed"
else
print_warning "No untagged images found"
fi
}
all_cleanup() {
medium_cleanup
print_step "Performing ALL cleanup (volumes and build cache)..."
# Remove volumes (WARNING: This will delete database data!)
print_warning "Removing volumes (this will DELETE database data!)"
read -p "Are you sure you want to delete ALL volumes? [y/N]: " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
print_step "Removing volumes..."
docker volume prune -f
print_success "Volumes removed"
else
print_warning "Skipping volume removal"
fi
# Clean build cache
print_step "Cleaning build cache..."
docker builder prune -f
print_success "Build cache cleaned"
# Remove all unused images (not just dangling)
print_step "Removing all unused images..."
docker image prune -a -f
print_success "All unused images removed"
}
# Main execution
main() {
print_header
local mode="--medium"
# Parse arguments
if [ $# -eq 0 ]; then
print_warning "No mode specified, using default: --medium"
else
case "$1" in
--soft)
mode="--soft"
;;
--medium)
mode="--medium"
;;
--all)
mode="--all"
;;
--help|-h)
print_usage
exit 0
;;
*)
print_error "Unknown option: $1"
print_usage
exit 1
;;
esac
fi
echo -e "Mode: ${GREEN}$mode${NC}"
echo -e "Project: ${YELLOW}auth-learning-app${NC}"
# Check if docker-compose.yml exists
if [ ! -f "docker-compose.yml" ]; then
print_error "docker-compose.yml not found in current directory"
echo "Please run this script from the project root directory"
exit 1
fi
# Check Docker is running
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running. Please start Docker daemon."
exit 1
fi
# Execute cleanup based on mode
case "$mode" in
--soft)
soft_cleanup
;;
--medium)
medium_cleanup
;;
--all)
all_cleanup
;;
esac
print_step "Cleanup completed!"
# Show remaining resources
echo -e "\n${BLUE}Remaining Docker resources:${NC}"
echo "Containers: $(docker ps -a -q --filter name=auth_learning 2>/dev/null | wc -l | tr -d ' ')"
echo "Images: $(docker images -q --filter reference='*auth-learning*' 2>/dev/null | wc -l | tr -d ' ')"
echo "Networks: $(docker network ls -q --filter name=auth_learning 2>/dev/null | wc -l | tr -d ' ')"
echo "Volumes: $(docker volume ls -q --filter name=auth_learning 2>/dev/null | wc -l | tr -d ' ')"
}
# Run main function
main "$@"