-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_cleanup.sh
More file actions
executable file
·109 lines (105 loc) · 3.4 KB
/
docker_cleanup.sh
File metadata and controls
executable file
·109 lines (105 loc) · 3.4 KB
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
105
106
107
108
#!/bin/bash
echo "Docker Cleanup Utility"
echo "======================="
echo "Choose an option:"
echo "1) Remove stopped containers"
echo "2) Remove unused images (dangling)"
echo "3) Remove unused volumes"
echo "4) Remove unused networks"
echo "5) Remove all unused Docker data (system prune)"
echo "6) Remove build cache"
echo "7) Remove all Docker data (system prune -a)"
echo "8) Remove specific container"
echo "9) Remove specific image"
echo "10) Remove specific volume"
echo "11) Remove Docker log files"
echo "12) Show Docker disk usage (docker system df)"
echo "13) Show disk usage alert"
echo "14) Exit"
echo "======================="
clean_docker() {
case $choice in
1)
echo "Removing stopped containers..."
docker container prune -f
;;
2)
echo "Removing unused images (dangling)..."
docker image prune -f
;;
3)
echo "Removing unused volumes..."
docker volume prune -f
;;
4)
echo "Removing unused networks..."
docker network prune -f
;;
5)
echo "Removing all unused Docker data (system prune)..."
read -p "Are you sure you want to proceed? (y/n): " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Action cancelled."
return 0
fi
docker system prune -f
;;
6)
echo "Removing Docker build cache..."
docker builder prune -f
;;
7)
echo "Removing all unused Docker data (system prune -a)..."
read -p "Are you sure you want to proceed? (y/n): " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Action cancelled."
return 0
fi
docker system prune -a -f
;;
8)
read -p "Enter the container name or ID to remove: " container_id
echo "Removing container $container_id..."
docker rm $container_id
;;
9)
read -p "Enter the image name or ID to remove: " image_id
echo "Removing image $image_id..."
docker rmi $image_id
;;
10)
read -p "Enter the volume name or ID to remove: " volume_id
echo "Removing volume $volume_id..."
docker volume rm $volume_id
;;
11)
read -p "Enter the container ID to remove logs for: " container_id
echo "Truncating log file for container $container_id..."
truncate -s 0 /var/lib/docker/containers/$container_id/*-json.log
;;
12)
echo "Showing Docker disk usage (docker system df)..."
docker system df
;;
13)
disk_usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$disk_usage" -gt 90 ]; then
echo "Warning: Disk usage is over 90%. Consider cleaning up Docker resources."
else
echo "Disk usage is normal ($disk_usage%)."
fi
;;
14)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice! Please select a valid option [1-14]."
;;
esac
}
while true; do
# Prompt for user input
read -p "Enter choice [1-14]: " choice
clean_docker
done