-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerhalt.in
executable file
·147 lines (105 loc) · 3.13 KB
/
dockerhalt.in
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
#!/bin/bash
#
#-------------------------------------
# Halt a container gracefully
# (C) M.A. O'Neill, Tumbling Dice 2021
#-------------------------------------
##############################
# Container details (editable)
##############################
docker_container=CONTAINER
vnc_screen=VNC_SCREEN
#--------------------
# Parse command line
#--------------------
if [ "$1" = "usage" ] || [ "$1" = "help" ] ; then
echo ""
echo " dockerhalt version 2,00"
echo " (C) M.A. O'Neill, Tumbling Dice, 2021-2022"
echo ""
echo " Usage: dockerhalt [usage | help] | [kill]"
echo ""
exit 1
fi
#---------------
# Display banner
#---------------
echo ""
echo " dockerhalt version 2.00"
echo " (C) M.A. O'Neill, Tumbling Dice, 2021-2022"
echo ""
#---------------------
# Is docker installed?
#---------------------
docker_installed=$(which dockerd | grep "no")
if [ "$docker_installed" != "" ] ; then
echo
echo " $docker_container.dockerBoot ERROR: docker is not installed on this host"
echo
exit 255
fi
#--------------------
# Is dockerd running?
#--------------------
dockerd_running=$(docker info |& head -1 |& awk '{print $1}' |& grep "Cannot")
if [ "$dockerd_running" != "" ] ; then
echo
echo " $docker_container.dockerBoot ERROR: dockerd is not running on this host"
echo
exit 255
fi
#----------------------
# Is container running?
#----------------------
echo ""
container_running=$(docker ps |& grep $docker_container |& awk '{print $1}' |& tr -d '\n')
if [ "$container_running" != "" ] ; then
###################################################
# Stop any services running in container (editable)
###################################################
#-----------
# VNC server
#-----------
if [ "$vnc_screen" != "" ] ; then
docker exec --user $docker_container -d $docker_container vncserver -kill $vnc_screen 1> /dev/null 2> /dev/null
echo " $docker_container.dockerhalt: VNC server ($vnc_screen) stopped"
fi
#-------------------
# Stop the container
#-------------------
docker stop $docker_container 1> /dev/null 2> /dev/null
#--------------------------------------
# If kill is specified delete container
#--------------------------------------
if [ "$1" = "kill" ] ; then
docker rm $docker_container 1> /dev/null 2> /dev/null
echo " $docker_container.dockerhalt: container stopped and removed"
echo ""
#-----------------------
# Don't remove container
#-----------------------
else
echo " $docker_container.dockerhalt: container stopped"
echo ""
fi
#-------------------------
# Container is not running
#-------------------------
#-----------------
# Remove container
#-----------------
elif [ "$1" = "kill" ] ; then
docker rm $docker_container 1> /dev/null 2> /dev/null
echo " $docker_container.dockerhalt: container is not running (and has been removed)"
echo ""
#----------------
# Leave container
#----------------
else
echo " $docker_container.dockerhalt: container is not running"
echo ""
fi
#------------
# We are done
#------------
exit 0