-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-container
120 lines (103 loc) · 3.05 KB
/
docker-container
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
#!/bin/sh
#####################################################################################################################
# Docker container script to start/stop and get container state #
#####################################################################################################################
#Create help function
printHelp() {
echo "######################################################################"
echo "# Usage: docker-container <container-name> <start/stop/status> #"
echo "# #"
echo "# Example: docker-container haproxy start #"
echo "# #"
echo "# List of available containers: docker ps -a #"
echo "######################################################################"
}
#Checks command line options
checkOptions() {
CONT="$1"
OPTI="$2"
if [ $# -eq 0 ] || [ -z $CONT ] || [ -z $OPTI ]; then
printHelp
exit 3
fi
}
#Function check options
checkOptions $@
#Set variables
START=start
STOP=stop
STATUS=status
STATE=`/usr/bin/docker ps |grep -w $CONT |wc -l`
#Set functions for exist and start/stop/status
containerExist(){
EXIST=`/usr/bin/docker ps -a|grep -w $CONT |wc -l`
if [ $EXIST -eq 0 ]; then
echo "Error: No such container: $CONT"
printHelp
exit 3
fi
}
startContainer(){
containerExist
if [ "$OPTI" == "$START" ] && [ $STATE -eq 0 ]; then
/usr/bin/docker start $CONT > /dev/null 2>&1
if [ $? -eq 0 ]; then
STATE=`/usr/bin/docker ps |grep -w $CONT |wc -l`
if [ $STATE -eq 1 ]; then
echo "Container $CONT is started"
exit 0
fi
else
echo "Can't start container $CONT"
printHelp
exit 3
fi
else
echo "Container $CONT is already running"
exit 3
fi
}
stopContainer(){
containerExist
if [ "$OPTI" == $STOP ] && [ $STATE -eq 1 ]; then
/usr/bin/docker stop $CONT > /dev/null 2>&1
if [ $? -eq 0 ]; then
STATE=`/usr/bin/docker ps |grep -w $CONT |wc -l`
if [ $STATE -eq 0 ]; then
echo "Container $CONT is stopped"
exit 0
fi
else
echo "Can't stop container $CONT"
printHelp
exit 3
fi
else
echo "Container $CONT is already stopped"
exit 3
fi
}
statusContainer(){
containerExist
if [ $STATE -eq 1 ]; then
echo "Container $CONT is running"
exit 0
elif [ $STATE -eq 0 ]; then
echo "Container $CONT is stopped"
exit 3
else
echo "Something is wrong with the container state or docker instance"
exit 3
fi
}
if [ "$OPTI" == "$START" ]; then
startContainer
elif [ "$OPTI" == "$STOP" ]; then
stopContainer
elif [ "$OPTI" == "$STATUS" ]; then
statusContainer
else
echo "Option -->$OPTI<-- not available"
printHelp
exit 3
fi