-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker_cleaner.sh
51 lines (47 loc) · 1.69 KB
/
docker_cleaner.sh
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
#!/bin/sh
usage () {
echo "Usage:\n\t sh docker_cleaner.sh <option>\n"
echo "Options: \n"
echo "\t-S, --soft\tSoft docker cleanup. Stopped containers, unused images, volumes \n\t\t\tand networks not related to running containers will be deleted.\n"
echo "\t-H, --hard\tHard docker cleanup. All containers will be stopped. \n\t\t\tAll containers, volumes and networks will be deleted.\n"
echo "\t-h, --help\tThis help.\n"
}
soft_cleanup() {
echo "Soft cleanup selected!\n"
echo "Stopped containers, unused images, volumes \nand networks not related to running containers will be deleted.\n"
read -p "Are you sure? (y/N) " yn
case "$yn" in
y|Y )
echo "Start soft cleanup!"
docker system prune -a -f 2> /dev/null
echo "Soft cleanup finished!"
;;
* )
echo "Soft cleanup canceled!"
;;
esac
}
hard_cleanup() {
echo "Hard cleanup selected!\n"
echo "All containers will be stopped. \nAll containers, volumes and networks will be deleted.\n"
read -p "Are you sure? (y/N) " yn
case "$yn" in
y|Y )
echo "Start hard cleanup!"
docker stop $(docker ps -a -q) 2> /dev/null
docker rm $(docker ps -a -q) 2> /dev/null
docker rmi --force $(docker images -a -q) 2> /dev/null
docker volume rm $(docker volume ls -q) 2> /dev/null
docker network rm $(docker network ls -q) 2> /dev/null
echo "Hard cleanup finished!"
;;
* )
echo "Hard cleanup canceled!"
;;
esac
}
case "$1" in
-S|--soft ) soft_cleanup ;;
-H|--hard ) hard_cleanup ;;
* ) usage ;;
esac