File size: 2,845 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash

# Docker Cleanup Script
# Removes unused Docker images, containers, volumes, and build cache
# Run this periodically to prevent disk space issues

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

echo -e "${BLUE}๐Ÿงน Docker Cleanup Utility${NC}"
echo "==========================================="
echo ""

# Show current disk usage
echo -e "${BLUE}๐Ÿ“Š Current Docker disk usage:${NC}"
docker system df
echo ""

# Parse arguments
AGGRESSIVE=false
if [[ "$1" == "--aggressive" || "$1" == "-a" ]]; then
    AGGRESSIVE=true
fi

if [ "$AGGRESSIVE" = true ]; then
    echo -e "${YELLOW}โš ๏ธ  Running AGGRESSIVE cleanup (removes ALL unused data)${NC}"
    echo ""
    echo "This will remove:"
    echo "  - All stopped containers"
    echo "  - All networks not used by at least one container"
    echo "  - All dangling images"
    echo "  - All unused images (not just dangling)"
    echo "  - All build cache"
    echo "  - All volumes not used by at least one container"
    echo ""
    read -p "Continue? (y/N): " -n 1 -r
    echo ""
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Cancelled."
        exit 1
    fi
    echo ""
    
    echo -e "${BLUE}๐Ÿ—‘๏ธ  Removing all unused Docker data...${NC}"
    docker system prune -a -f --volumes
    
else
    echo -e "${BLUE}๐Ÿ—‘๏ธ  Running standard cleanup...${NC}"
    echo ""
    echo "This will remove:"
    echo "  - All stopped containers"
    echo "  - All networks not used by at least one container"
    echo "  - All dangling images"
    echo "  - Build cache older than 24 hours"
    echo ""
    
    # Remove stopped containers
    echo -e "${YELLOW}Removing stopped containers...${NC}"
    docker container prune -f
    
    # Remove dangling images
    echo -e "${YELLOW}Removing dangling images...${NC}"
    docker image prune -f
    
    # Remove unused networks
    echo -e "${YELLOW}Removing unused networks...${NC}"
    docker network prune -f
    
    # Remove build cache older than 24 hours
    echo -e "${YELLOW}Removing old build cache...${NC}"
    docker builder prune -f --filter "until=24h"
fi

echo ""
echo -e "${GREEN}โœ… Cleanup complete!${NC}"
echo ""

# Show updated disk usage
echo -e "${BLUE}๐Ÿ“Š Updated Docker disk usage:${NC}"
docker system df
echo ""

echo "==========================================="
echo -e "${GREEN}๐Ÿ’ก Tips:${NC}"
echo ""
echo "Run this script regularly to prevent disk space issues:"
echo ""
echo "  Standard cleanup (safe):"
echo "    ./docker-cleanup.sh"
echo ""
echo "  Aggressive cleanup (removes ALL unused data):"
echo "    ./docker-cleanup.sh --aggressive"
echo ""
echo "Add to crontab for weekly cleanup:"
echo "    0 2 * * 0 /path/to/docker-cleanup.sh > /dev/null 2>&1"
echo ""
echo "==========================================="