-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-gc.sh
executable file
·165 lines (122 loc) · 2.66 KB
/
docker-gc.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
set -e
set -o noglob
TMP_DIR=
# --- helper functions for logs ---
info()
{
echo '[INFO] ' "$@"
}
debug()
{
echo '[DEBUG] ' "$@"
}
warn()
{
echo '[WARN] ' "$@" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
setup() {
TMP_DIR=$(mktemp -d -t docker-gc.XXXXXXXXXX)
touch ${TMP_DIR}/containers.reap
touch ${TMP_DIR}/images.reap
touch ${TMP_DIR}/volumes.reap
cleanup() {
code=$?
set +e
trap - EXIT
rm -rf ${TMP_DIR}
exit $code
}
trap cleanup INT EXIT
}
allowed_commands() {
warn 'Only available commands are:'
warn '--purge-all'
}
get_disk_size() {
CURRENTUSAGEPERCENT=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
CURRENTUSAGE=$(df -h / | awk 'NR==2 {print $3}')
TOTAL=$(df -h / | awk 'NR==2 {print $2}')
info "${CURRENTUSAGE}/${TOTAL} / $CURRENTUSAGEPERCENT%"
}
get_disk_size_bytes() {
local result=$(df -B1 --output=used . | awk 'NR==2 {print $1}')
echo $result
}
get_all_containers() {
# get all
local result=$(docker ps -a -q --no-trunc | sort | uniq )
echo $result
}
get_all_images() {
local result=$(docker images --no-trunc --format "{{.ID}}")
echo $result
}
get_all_volumes() {
local result=$(docker volume ls --filter "dangling=true" -q)
echo $result
}
remove_containers() {
xargs -n 1 docker rm -f --volumes=true < ${TMP_DIR}/containers.reap &>/dev/null || true
}
remove_images() {
xargs -n 1 docker rmi -f < ${TMP_DIR}/images.reap &>/dev/null || true
}
remove_volumes() {
xargs -n 1 docker volume rm < ${TMP_DIR}/volumes.reap &>/dev/null || true
}
purge_all() {
read -p "This is going to destroy everything, are you sure? (yes/no): " response
if [[ $response == "no" ]]; then
info "Phew! Disaster averted. Exiting..."
exit 0
elif [[ "$response" != "yes" ]]; then
warn "Invalid response. Please enter 'yes' or 'no'."
exit 0
fi
setup
local before_cleanup=$(get_disk_size_bytes)
info "Disk size before cleanup"
get_disk_size
get_all_containers > ${TMP_DIR}/containers.reap
get_all_images > ${TMP_DIR}/images.reap
get_all_volumes > ${TMP_DIR}/volumes.reap
remove_containers
remove_images
remove_volumes
docker system prune -a -f &>/dev/null
docker builder prune -a -f &>/dev/null
info "Disk size after cleanup"
get_disk_size
local after_cleanup=$(get_disk_size_bytes)
local difference=$((before_cleanup - after_cleanup))
if [[ $difference -gt 0 ]]; then
local saved=$(numfmt --to=iec-i --suffix=B --format='%.1f' $difference)
info "You saved ${saved}"
else
info "Nothing to cleanup"
fi
}
entrypoint() {
case "$1" in
(-*)
case "$1" in
--purge-all) purge_all "$@";;
*)
allowed_commands
esac
;;
"")
allowed_commands
;;
*)
esac
}
{
entrypoint "$@"
}