-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.sh
199 lines (147 loc) · 3.64 KB
/
docker.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
# set -x
# set -u
set -e
ME=$(basename "$0")
MYDIR=$(dirname "$0")
MYDIR=$(cd "$MYDIR"/.. && pwd)
export ME
#####################################################
#
# Function
#
#####################################################
list_containers () {
docker ps -a
}
list_images () {
docker images
}
list_unused_containers () {
docker ps -a -q -f status=exited
}
list_unused_volumes () {
docker volume ls -qf dangling=true
}
list_unused_images () {
docker images -f dangling=true -q
}
list_networks () {
docker network ls -f 'driver=bridge'
}
rm_unused_containers () {
#docker rm "$1"
docker ps -a -q -f status=exited | xargs docker stop
docker ps -a -q -f status=exited | xargs docker rm
}
rm_unused_volumes () {
docker volume ls -qf dangling=true | xargs docker volume rm
}
rm_unused_images () {
docker images -f dangling=true -q | xargs docker rmi
}
rm_unused_networks () {
docker network prune -f
}
is_old_mutable_tags () {
which remove_old_mutable_tags.sh
}
stop_docker-registry () {
docker stop docker-registry
}
garbage_docker-registry () {
docker run -it --name gc --rm --volumes-from docker-registry registry:2 garbage-collect /etc/docker/registry/config.yml
}
start_docker-registry () {
docker start docker-registry
}
info () {
echo "
#####################################################
#
# "$1"
#
#####################################################
"
}
#####################################################
#
# Main
#
#####################################################
OPTSPECS[a]="IMAGES:yes: List Images:no"
OPTSPECS[b]="CONTAINERS:yes: List Containers:no"
OPTSPECS[c]="UCONTAINERS:yes: List Unused Containers:no"
OPTSPECS[d]="UVOLUMES:yes: List Unused Volumes:no"
OPTSPECS[e]="UIMAGES:yes: List Unused Images:no"
OPTSPECS[f]="LIST:yes: List All List:no"
OPTSPECS[g]="REMOVE:yes: Remove All List:no"
OPTSPECS[i]="GARBAGE:yes: Docker Garbage Collector:no"
export OPTSPECS
get_options "$@"
if [[ "$IMAGES" != no ]]; then
info 'list images'
list_images
fi
if [[ "$CONTAINERS" != no ]]; then
info 'list containers'
list_containers
fi
if [[ "$UCONTAINERS" != no ]]; then
info 'list unused containers'
list_unused_containers
fi
if [[ "$UVOLUMES" != no ]]; then
info 'list unused volumes'
list_unused_volumes
fi
if [[ "$UIMAGES" != no ]]; then
info 'list unused images'
list_unused_images
fi
if [[ "$LIST" != no ]]; then
info 'list images'
list_images
info 'list unused images'
list_unused_images
info 'list containers'
list_containers
info 'list unused containers'
list_unused_containers
info 'list unused volumes'
list_unused_volumes
info 'list bridge networks'
list_networks
fi
if [[ "$REMOVE" != no ]]; then
if [[ -n $(list_unused_containers) ]]; then
info 'remove unused containers'
rm_unused_containers
fi
if [[ -n $(list_unused_volumes) ]]; then
info 'remove unused volumes'
rm_unused_volumes
fi
if [[ -n $(list_unused_images) ]]; then
info 'remove unused images'
rm_unused_images
fi
if [[ -n $(list_networks) ]]; then
info 'remove unused networks'
rm_unused_networks
fi
fi
if [[ "$GARBAGE" != no ]]; then
if [[ -n $(is_old_mutable_tags) ]]; then
info 'remove_old_mutable_tags'
remove_old_mutable_tags.sh
info 'stop docker-registry'
stop_docker-registry
info 'garbage-collect docker-registry'
garbage_docker-registry
info 'start docker-registry'
start_docker-registry
else
info 'remove_old_mutable_tags.sh is not installed'
fi
fi