-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerclean
executable file
·178 lines (165 loc) · 4.45 KB
/
dockerclean
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
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
summary='Remove images or containers'
usage="Usage: dockerclean [OPTIONS]"
# global variables
dryRun=true
dangling=false
grepContainer=false
grepContainerPattern=''
grepImage=false
grepImagePattern=''
exitedContainers=false
function displayUsageAndSummary {
printf "%s\n\n%s\n" "$usage" "$summary"
}
function displayHelp {
displayUsageAndSummary
echo ''
declare -A optionsTable
local numFlags=6
optionsTable["0,0"]='-c PATTERN'
optionsTable["0,1"]='Remove all containers matched by PATTERN'
optionsTable["1,0"]='-d'
optionsTable["1,1"]='Remove all images tagged as <none>'
optionsTable["2,0"]='-r'
optionsTable["2,1"]='If the -r option is not given, display all images or containers that would be removed'
optionsTable["3,0"]='-i PATTERN'
optionsTable["3,1"]='Remove all images matched by PATTERN'
optionsTable["5,0"]='-e'
optionsTable["5,1"]='Remove all exited containers'
optionsTable["4,0"]='-h'
optionsTable["4,1"]='Print usage and options'
i=0
while [ $i -lt $numFlags ]; do
printf '%4s'
printf "%-14s%s\n" "${optionsTable["$i",0]}" "${optionsTable["$i",1]}"
((i = i + 1))
done
}
function displayHelpPrompt {
echo "See 'dockerutil clean -h' for more information"
}
function gatherDanglingImagesToRemove {
if ! "$dangling"; then
echo ''
return 0
fi
docker images | grep none
}
function removeDanglingImages {
if ! "$dangling"; then
echo ''
return 0
fi
if [ -z "$1" ]; then
echo 'No dangling images to remove'
return 0
fi
docker rmi -f $(docker images | grep none | awk '{print $3}')
}
function gatherImagesToRemove {
if ! "$grepImage"; then
echo ''
return 0
fi
docker images | grep "$grepImagePattern"
}
function removeImages {
if ! "$grepImage"; then
echo ''
return 0
fi
if [ -z "$1" ]; then
echo 'No matching images to remove'
return 0
fi
docker rmi -f $(docker images | grep $grepImagePattern | awk '{print $3}')
}
function gatherContainersToRemove {
if "$grepContainer" && "$exitedContainers"; then
docker ps -a | grep -E "$grepContainerPattern|Exited"
elif "$grepContainer"; then
docker ps -a | grep -E "$grepContainerPattern"
elif "$exitedContainers"; then
docker ps -a | grep -E "Exited"
else
echo ''
fi
}
function removeContainers {
if ! "$grepContainer" && ! "$exitedContainers"; then
echo ''
return 0
fi
if [ -z "$1" ]; then
echo 'No matching containers to remove'
return 0
fi
docker rm -f $(echo "$1" | awk '{print $1}')
}
while getopts ":c:deri:h" option; do
case "$option" in
h)
displayHelp
exit 0
;;
r)
dryRun=false
;;
d)
dangling=true
;;
c)
grepContainer=true
grepContainerPattern="$OPTARG"
;;
e)
exitedContainers=true
;;
i)
grepImage=true
grepImagePattern="$OPTARG"
;;
\?)
echo "invalid option: -$OPTARG" >&2
echo ''
displayHelpPrompt
exit 1
;;
:)
echo "option -$OPTARG requires an argument"
echo ''
displayHelpPrompt
exit 1
;;
esac
done
if ! { "$dangling" || "$grepContainer" || "$grepImage" || "$exitedContainers"; } then
echo 'Nothing will be removed: no removal strategy specified'
echo ''
displayHelpPrompt
else
images=$(gatherImagesToRemove)
danglingImages=$(gatherDanglingImagesToRemove)
containers=$(gatherContainersToRemove)
if "$dryRun"; then
if [ -z "$images" ] && [ -z "$danglingImages" ] && [ -z "$containers" ]; then
printf 'Nothing to remove\n'
else
printf 'Rerun with the -r option to remove the following:\n'
fi
if [ ! -z "$images" ]; then
printf "\nImages:\n%s\n" "$images"
fi
if [ ! -z "$danglingImages" ]; then
printf "\nDangling Images:\n%s\n" "$danglingImages"
fi
if [ ! -z "$containers" ]; then
printf "\nContainers:\n%s\n" "$containers"
fi
else
removeImages "$images"
removeDanglingImages "$danglingImages"
removeContainers "$containers"
fi
fi