-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunDocker.sh
executable file
·279 lines (235 loc) · 6.32 KB
/
runDocker.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/sh
# Gustavo Luszczynski <[email protected]>
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# dnsmasq container name
DNSMASQ_CONTAINER_NAME="dnsmasq"
# docker user
DOCKER_USER="luszczynski"
# Container Operation System
OS_CONTAINER="centos7"
function cleanup() {
docker rmi $(docker images | grep "^<none>" | awk '{print $3}') 2>/dev/null
docker rm $(docker ps -a | awk '{print $1}' | sed "1 d") 2>/dev/null
}
function updateDNS() {
# Backup
cp /etc/resolv.conf{,.orig}
# create resolv.conf
echo "nameserver $1" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
}
# $1: Parameters
# $2: Container name
# $3: Image name
# $4: Container parameter
function startContainer() {
cleanup 1> /dev/null
IP_DNS=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $DNSMASQ_CONTAINER_NAME 2> /dev/null)
if [ "x$IP_DNS" == "x" ]; then
echo "dnsmasq not started. Starting now..."
docker run --privileged -d --name $DNSMASQ_CONTAINER_NAME -h $DNSMASQ_CONTAINER_NAME $DOCKER_USER/$OS_CONTAINER-$DNSMASQ_CONTAINER_NAME 2> /dev/null
IP_DNS=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $DNSMASQ_CONTAINER_NAME)
updateDNS "$IP_DNS"
fi
CMD_START="docker run --privileged $1 -d --name $2 --volumes-from $DNSMASQ_CONTAINER_NAME --dns $IP_DNS -v /etc/timezone:/etc/timezone:ro -v /tmp/share:/tmp/share";
if [ "x$4" == "x" ]; then
CMD_START="$CMD_START -h $2"
else
CMD_START="$CMD_START -h $4"
fi
CMD_START="$CMD_START $DOCKER_USER/$OS_CONTAINER-$3 $4"
$CMD_START;
}
function stopContainer() {
docker stop $1
cleanup
}
function stopAll() {
docker stop $(docker ps --filter 'status=running' | awk '{print $1}' | sed "1 d") 2>/dev/null
cleanup
}
function statusContainer() {
status=$(docker ps | grep $1 2> /dev/null)
if [ "x$status" == "x" ]; then
echo "Container is NOT running"
else
echo "Container is running"
fi
}
function killContainer() {
docker kill $1
cleanup
}
function killAll() {
docker kill $(docker ps --filter 'status=running' | awk '{print $1}' | sed "1 d") 2>/dev/null
cleanup
}
function logContainer() {
docker logs -f $1
}
function attachContainer() {
docker exec -it $1 bash
}
function bashContainer() {
docker run -ti --privileged $1 --rm --name $2 $DOCKER_USER/$OS_CONTAINER-$3 /bin/bash
}
# $1 image name
# $2 path to Dockerfile
function buildImage() {
PARAM=$1
[[ $1 != $OS_CONTAINER* ]] || PARAM=$(echo $1 | cut -d "-" -f2,3,4,5)
docker build --rm=true --force-rm=true -t $DOCKER_USER/$OS_CONTAINER-$PARAM $2
if [ "$?" == 0 ]; then
cleanup
fi
}
function buildAll() {
base_images=$(ls base)
for image in $base_images; do
CONTAINER_DIR=$(find . -name "$image" | sed 's/.\///')
buildImage $image $CONTAINER_DIR
done
products_images=$(ls products)
for image in $products_images; do
CONTAINER_DIR=$(find . -name "$image" | sed 's/.\///')
buildImage $image $CONTAINER_DIR
done
demo_images=$(ls demos)
for image in $demo_images; do
CONTAINER_DIR=$(find . -name "$image" | sed 's/.\///')
buildImage $image $CONTAINER_DIR
done
}
function resume() {
echo
}
function removeAllImages() {
docker rmi $(docker images | grep $DOCKER_USER | awk '{print $1}')
}
function usage() {
echo
echo "Usage: $0 ( start | stop | status | kill | log | attach | bash | build ) <image name> <instance name>"
echo "Usage: $0 ( stopall | killall | buildall | clean | removeimages )"
echo "============================================="
echo
echo "start <image name> <instance name> start container"
echo "stop <instance name> stop container"
echo "kill <instance name> kill container"
echo "status <instance name> show if container is running or not"
echo "log <instance name> show container standard output"
echo "attach <instance name> access container (similar to ssh)"
echo "bash <image name> start container running /bin/bash"
echo "build <image name> build container image"
echo
echo "<image name> is equal to Dockerfile's folder name."
echo "<instance name> is the name you give to your container."
echo "If none is set then instance name will be image name. Only some images accept random names (eap-host and jdg)"
echo
echo "eg: $0 start eap-domain"
echo " $0 log eap-domain"
echo "eg2: $0 start eap-host host01"
echo " $0 attach host01"
echo "eg3: $0 start eap-host host02"
echo " $0 kill host02"
echo
echo
echo "Usage: $0 ( stopall | killall | buildall | clean | removeimages )"
echo "=============================================="
echo
echo "stopall stop all containers"
echo "killall kill all containers"
echo "buildall build all images"
echo "clean remove stopped containers (docker ps -a)"
echo "removeimages remova all ${DOCKER_USER}'s images"
exit 0
}
if [ "$#" == 0 ] ; then
usage
exit 1
fi
# Path to container
CONTAINER_DIR=$(find . -name "$2" | sed 's/.\///')
# Image name. It's the same name of the container dir
IMAGE_NAME=$(echo $CONTAINER_DIR | awk -F/ '{ print $NF }')
# Instance name. Only used when image support multiples instances
INSTANCE_NAME=$(docker ps | grep "$2" | awk -F" " '{print $NF}')
MULTIPLE_INSTANCE=false
if [ "x$3" != "x" ]; then
MULTIPLE_INSTANCE=true
fi
case "$1" in
start)
if [ $MULTIPLE_INSTANCE == true ]; then
startContainer "$CMD_LINE" $3 $IMAGE_NAME $3
else
startContainer "$CMD_LINE" $IMAGE_NAME $IMAGE_NAME
fi
;;
stop)
if [ $MULTIPLE_INSTANCE == true ]; then
stopContainer $3
else
stopContainer $INSTANCE_NAME
fi
;;
resume)
resume $INSTANCE_NAME
;;
status)
if [ $MULTIPLE_INSTANCE == true ]; then
statusContainer $3
else
statusContainer $INSTANCE_NAME
fi
;;
kill)
if [ $MULTIPLE_INSTANCE == true ]; then
killContainer $3
else
killContainer $INSTANCE_NAME
fi
;;
log)
logContainer $2
;;
attach)
if [ $MULTIPLE_INSTANCE == true ]; then
attachContainer $3
else
attachContainer $INSTANCE_NAME
fi
;;
bash)
if [ $MULTIPLE_INSTANCE == true ]; then
bashContainer "$CMD_LINE" $3 $IMAGE_NAME
else
bashContainer "$CMD_LINE" $IMAGE_NAME $IMAGE_NAME
fi
;;
build)
buildImage $IMAGE_NAME $CONTAINER_DIR
;;
stopall)
stopAll
;;
killall)
killAll
;;
buildall)
buildAll
;;
clean)
cleanup
;;
removeimages)
removeAllImages
;;
--help)
usage
;;
*)
usage
esac