-
Notifications
You must be signed in to change notification settings - Fork 10
/
docker.sh
executable file
·259 lines (228 loc) · 5.05 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
set -eu
set -o pipefail
if [ -z "${1-}" ]; then
echo "Please input {action} to execute command"
echo "Example: [start|stop|restart|build|config|exec|logs|ps|clean|destroy|test-build]"
echo "Usage: $ docker.sh {action}"
exit;
else
case "${1-}" in
build|pull|exec)
if [ -z "${2-}" ]; then
echo "Input {service} to execute command if you dont want perform all"
echo "Example: [php|nginx|mysql|cert|redis]"
echo "Usage: $ docker.sh {action} {service}"
fi
;;
esac
fi
echo "✹✹✹ Executing command ✹✹✹"
echo "UTC Time: $(TZ=UTC date ' %Y-%m-%d %H:%M:%S')"
echo "VN Time: $(TZ=Asia/Bangkok date ' %Y-%m-%d %H:%M:%S')"
echo "JST Time: $(TZ=Asia/Tokyo date ' %Y-%m-%d %H:%M:%S')"
export USERID=$(id -u)
export GROUPID=$(id -g)
command_action=${1-}
command_service=${2-}
command_service_run=${3-}
docker_compose="/usr/local/bin/docker-compose"
docker_dev_yml="docker-compose.dev.yml"
change_context_dir() {
echo "------ Change exec command context dir ------"
context_dir=$(dirname "$0")
# Change execute context to project's directory
cd $context_dir
}
create_volume() {
name=${1-}
volume_existing=$(docker volume ls --filter name=${name} --format '{{ .Name }}')
echo "------ Create $name volume ------"
if [ -z "$volume_existing" ]; then
docker volume create $name
else
echo "$name volume existing"
fi
}
remove_volume() {
name=${1-}
volume_existing=$(docker volume ls --filter name=${name} --format '{{ .Name }}')
echo "------ Remove $name volume ------"
if [ -z "$volume_existing" ]; then
docker volume rm $name
else
echo "$name volume is not existing"
fi
}
run_each_command() {
service=${1:-""}
echo $2
shift
cmds=("$@")
for (( i = 0; i < ${#cmds[@]}; i )); do
${cmds[$i]} ${service}
done
}
docker_wrapper() {
message=${1:-}
service=${2:-}
shift 2
cmds=("$@")
case "$service" in
"")
echo "------ $message [ALL] ------"
run_each_command "" "${cmds[@]}"
;;
*)
echo "------ $message [$service] ------"
run_each_command "$service" ${cmds[@]}
;;
esac
}
docker_build() {
message="Re-build"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml build --force-rm")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_config() {
message="Validate"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml config")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_up() {
message="Up"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml up")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_stop() {
message="Stop"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml stop")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_down() {
message="Down"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml down")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_restart() {
message="Restart"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml restart")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
docker_exec() {
message="Exec"
service=${1:-}
command=${2:-}
if [ -z "$command" ]; then
command="/bin/bash"
fi
$docker_compose -f $docker_dev_yml exec "$service" $command
}
docker_remove_dangling_images() {
echo "------ Removing all dangling images ------"
dangling_images=$(docker images -f "dangling=true" -q)
if [ -n "$dangling_images" ]; then
docker rmi $(docker images -f "dangling=true" -q)
fi
}
docker_logs() {
message="Log"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml logs")
docker_wrapper $message $service $cmds
}
docker_ps() {
message="Process Status"
service=${1:-}
cmds=("$docker_compose -f $docker_dev_yml ps")
docker_wrapper "$message" "$service" "${cmds[@]}"
}
action_start() {
change_context_dir
create_volume "mysql_data"
create_volume "composer_cache"
docker_up $command_service
}
action_stop() {
change_context_dir
docker_down $command_service
}
action_restart() {
change_context_dir
docker_restart $command_service
}
action_exec() {
change_context_dir
docker_exec $command_service $command_service_run
}
action_ps() {
change_context_dir
docker_ps $command_service
}
action_config() {
change_context_dir
docker_config
}
action_build() {
change_context_dir
docker_build
}
action_logs() {
change_context_dir
docker_logs
}
action_clean() {
change_context_dir
docker_remove_dangling_images
}
action_destroy() {
change_context_dir
docker_down $command_service
remove_volume "mysql_data"
remove_volume "composer_cache"
}
case "$command_action" in
"start")
action_start
;;
"stop")
action_stop
;;
"restart")
action_restart
;;
"build")
action_build
;;
"config")
action_config
;;
"exec")
action_exec
;;
"logs")
action_logs
;;
"ps")
action_ps
;;
"clean")
action_clean
;;
"destroy")
action_destroy
;;
"test-build")
action_destroy
action_build
action_start
;;
*)
echo "Unknown action: $command_action"
esac