-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerboot.in
executable file
·626 lines (452 loc) · 13.2 KB
/
dockerboot.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/bin/bash
#
#-------------------------------------------
# Start a docker container
# (C) M.A. O'Neill, Tumbling Dice, 2021-2023
#
# $1 overide resetting of VNC password
#-------------------------------------------
##############################
# Container details (editable)
##############################
mem_size="MEM_SIZE"
shm_size="SHM_SIZE"
docker_repository="REPOSITORY"
docker_image="IMAGE"
docker_container="CONTAINER"
container_running=TRUE
remove="REMOVE"
ip_addr="IP_ADDR"
ssh_port="SSH_PORT"
smtp_port="SMTP_PORT"
http_port="HTTP_PORT"
https_port="HTTPS_PORT"
reset_vnc_passwd="RESET_VNC_PASSWD"
vnc_port="VNC_PORT"
vnc_screen="VNC_SCREEN"
vnc_id="VNC_ID"
vnc_geometry="VNC_GEOMETRY"
hostLocal="HOSTLOCAL"
hostHome="HOSTHOME"
#-------------------
# Handler for SIGINT
#-------------------
function abort_script()
{
#----------------------------------------------
# If container is running stop it and remove it
#----------------------------------------------
if [ $container_running = TRUE ] ; then
docker stop $docker_container
docker rm $docker_container
fi
echo ""
echo " *** aborted (SIGINT)"
echo ""
exit 255
}
trap abort_script SIGINT
#-------------------
# Parse command line
#-------------------
if [ "$1" = "usage" ] || [ "$1" = "help" ] ; then
echo ""
echo " dockerboot version 2.00"
echo " (C) M.A. O'Neill, Tumbling Dice, 2021-2023"
echo ""
echo " Usage: $docker_container.dockerboot [usage | help] | [vncpasswd | novncpasswd]"
echo ""
exit 1
#-------------------------
# Overide VNC password set
#-------------------------
elif [ "$vnc_port" != "" ] ; then
if [ "$1" = "vncpasswd" ] ; then
reset_vnc_passwd=TRUE
elif [ "$1" = "novncpasswd" ] ; then
reset_vnc_passwd=FALSE
elif [ "$1" != "" ] ; then
echo ""
echo "$docker_container.dockerboot ERROR: 'vncpasswd' or 'novncpasswd' expected"
echo ""
exit 1
fi
fi
#---------------
# Display banner
#---------------
echo ""
echo " dockerboot version 2.00"
echo " (C) M.A. O'Neill, Tumbling Dice, 2021-2023"
echo ""
#----------------------------------
# User i.d (to run container under)
#----------------------------------
user=$(ask " User i.d. (to run container ssh daemon/vncserver under)")
if [ "$user" = q ] || [ "$user" = quit ] ; then
echo ""
echo " *** aborted (quit)"
echo ""
exit 255
elif [ $user = "" ]; then
user=$(whoami)
echo " $docker_congtainer.dockerboot WARNING: running container ssh daemon/vncserver under current user i.d. ($user)"
echo ""
fi
#------------------------------------------
# Are the ports this container wants to use
# already in use?
#------------------------------------------
#-----------------
# Any i.p. address
#-----------------
if [ "$ip_addr" = "" ] ; then
eff_ip_addr=0.0.0.0
#----------------------
# Specific i.p. address
#----------------------
else
eff_ip_addr=$ip_addr
fi
#---------
# SSH port
#---------
if [ "$ssh_port" != "" ] ; then
if [ "$(lsof -i -P -n |& grep $eff_ip_addr:$ssh_port |& grep LISTEN |& tr -d '\n')" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: ssh port ($ssh_port) in use"
echo ""
exit 255
fi
fi
#----------
# SMTP port
#----------
if [ "$smtp_port" != "" ] ; then
if [ "$(lsof -i -P -n |& grep $eff_ip_addr:$smtp_port |& grep LISTEN |& tr -d '\n')" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: smtp port ($smtp_port) in use"
echo ""
exit 255
fi
fi
#----------
# http port
#----------
if [ "$http_port" != "" ] ; then
if [ "$(lsof -i -P -n |& grep $eff_ip_addr:$http_port |& grep LISTEN |& tr -d '\n')" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: http port ($http_port) in use"
echo ""
exit 255
fi
fi
#-----------
# https port
#-----------
if [ "$https_port" != "" ] ; then
if [ "$(lsof -i -P -n |& grep $eff_ip_addr:$https_port |& grep LISTEN |& tr -d '\n')" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: https port ($https_port) in use"
echo ""
exit 255
fi
fi
#---------
# VNC port
#---------
if [ "$vnc_port" != "" ] ; then
if [ "$(lsof -i -P -n | grep eff_ip_addr:$vnc_port | grep LISTEN |& tr -d '\n')" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: vnc port ($vnc_port) in use"
echo ""
exit 255
fi
fi
#---------------------
# 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?
#----------------------
container_running=$(docker ps |& grep $docker_container |& awk '{print $1}' |& tr -d '\n')
if [ "$container_running" != "" ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: container $docker_container is already running"
echo ""
exit 255
fi
#----------------------
# Does container exist?
#----------------------
container_exists=$(docker ps -a |& grep $docker_container |& awk '{print $1}' |& tr -d '\n')
#-----------------------------------
# Found container - so just start it
#-----------------------------------
if [ "$container_exists" != "" ] ; then
docker start $docker_container 1> /dev/null 2> /dev/null
docker exec $docker_container bash -c "$startup_command_set" 1> /dev/null 2> /dev/null
#-------------------------
# Did the container start?
#-------------------------
if [ $? -ne 0 ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: failed to restart container $docker_container"
echo ""
exit 255
#------------------
# Container running
#------------------
else
container_running=TRUE
fi
#---------------------------------------
# Load image into container and start it
#---------------------------------------
else
#--------------------
# Set image arguments
#--------------------
#---------------------------------
# Remove container on exit if TRUE
#---------------------------------
if [ "$remove" = TRUE ] ; then
removeArg="--rm"
else
removeArg=""
fi
#-----------------------
# Export /home from host
#-----------------------
if [ "$hostHome" != "" ] ; then
hostHomeArg="-v $hostHome"
else
hostHomeArg=""
fi
#----------------------------
# Export /usr/local from host
#----------------------------
if [ "$hostLocal" != "" ] ; then
hostLocalArg="-v $hostLocal"
else
hostLocalArg=""
fi
#---------------------
# Container IP address
#---------------------
if [ "$ip_addr" != "" ] ; then
ip_addrArg="--ip $ip_addr"
else
ip_addrArg=""
fi
#---------------------
# Container VNC screen
#---------------------
if [ "$vnc_screen" != "" ] ; then
vnc_portArg="-p $eff_ip_addr:${vnc_port}:${vnc_port}"
else
vnc_portArg=""
fi
#---------------------
# Container web server
#---------------------
if [ "$http_port" != "" ] ; then
http_portArg="-p $eff_ip_addr:${http_port}:80"
else
http_portArg=""
fi
#-----------------------------
# Container sercure web server
#-----------------------------
if [ "$https_port" != "" ] ; then
https_portArg="-p $eff_ip_addr:${https_port}:433"
else
https_portArg=""
fi
#---------------------
# Container ssh access
#---------------------
if [ "$ssh_port" != "" ] ; then
ssh_portArg="-p $eff_ip_addr:${ssh_port}:22"
else
ssh_portArg=""
fi
#----------------------
# Container SMTP access
#----------------------
if [ "$smtp_port" != "" ] ; then
smtp_portArg="-p $eff_ip_addr:${smtp_port}:25"
else
smtp_portArg=""
fi
#-------------------------------
# Container specific command set
#-------------------------------
if [ "$ssh_port" = "" ] ; then
startup_command_set="/etc/rc.local; /usr/bin/sleep infinity"
else
startup_command_set="/etc/rc.local; /usr/sbin/sshd -D"
fi
#----------------------
# Load image and run it
#----------------------
docker run --cap-add SYS_PTRACE,PERFMON \
--security-opt seccomp=unconfined \
--memory=$mem_size \
-v /dev/shm --tmpfs /dev/shm:rw,nosuid,nodev,exec,size=$shm_size \
$removeArg \
$hostHomeArg \
$hostLocalArg \
-d \
$ip_addrArg \
$ssh_portArg \
$smtp_portArg \
$http_portArg \
$https_portArg \
$vnc_portArg \
-h $docker_container \
--name $docker_container \
${docker_repository}:${docker_image} bash -c "$startup_command_set" 1> /dev/null 2> /dev/null
#-------------------------
# Did the container start?
#-------------------------
if [ $? -ne 0 ] ; then
#----------------------
# Clear service entries
#----------------------
echo ""
echo " $docker_container.dockerboot ERROR: failed to start container"
echo ""
exit 255
#------------------
# Container running
#------------------
else
container_running=TRUE
fi
fi
######################################################
# Start service (edit to start any ones which we need)
######################################################
#--------------------------------------------------
# VNC server (run under identity of contained user)
#--------------------------------------------------
if [ "$vnc_screen" != "" ] ; then
#-------------------------------
# Set VNC password if we need to
#-------------------------------
docker exec --user $user $docker_container havevncpasswd 1> /dev/null 2> /dev/null
if [ $? -ne 0 ] ; then
echo " VNC password required"
echo ""
docker exec --interactive --tty --user $user $docker_container vncpasswd
echo ""
elif [ "$reset_vnc_passwd" = TRUE ] ; then
echo " VNC password reset forced"
echo ""
docker exec --interactive --tty --user $user $docker_container vncpasswd
echo ""
fi
#------------------------
# Try to start VNC server
#------------------------
#-------------------------------
# Remove junk from previous runs
#-------------------------------
docker exec --user root $docker_container /bin/bash -c "rm -rf /tmp/.X11-unix" 1> /dev/null 2> /dev/null
docker exec --user $user $docker_container /bin/bash -c "rm /tmp/.X{$vnc_id}-lock" 1> /dev/null 2> /dev/null
#--------------------
# Start this instance
#--------------------
docker exec --user $user $docker_container /bin/bash -c "export USER=$user; vncserver -geometry $vnc_geometry $vnc_screen" 1> /dev/null 2> /dev/null
#-----------------------
# Did VNC service start?
#-----------------------
if [ $? -ne 0 ] ; then
echo ""
echo " $docker_container.dockerboot ERROR: failed to start vnc service"
echo ""
exit 255
fi
fi
#------------
# We are done
#------------
echo ""
echo " $docker_container.dockerboot: container started"
echo ""
#---------------------------
# Display container settings
#---------------------------
echo " docker repository: $docker_repository"
echo " docker image : $docker_image"
echo " docker container : $docker_container"
#-----------
# IP address
#-----------
if [ "$ip_addr" != "" ] ; then
echo " IP address : $ip_addr"
fi
#---------
# ssh port
#---------
if [ "$ssh_port" != "" ] ; then
echo " ssh port : $ssh_port"
fi
#----------
# SMTP port
#----------
if [ "$smtp_port" != "" ] ; then
echo " smtp port : $smtp_port"
fi
#----------
# http port
#----------
if [ "$http_port" != "" ] ; then
echo " http port : $http_port"
fi
#-----------
# https port
#-----------
if [ "$https_port" != "" ] ; then
echo " https port : $https_port"
fi
#-----------------------------------
# Only display details of VNC server
# if one was started
#-----------------------------------
if [ "$vnc_screen" != "" ] ; then
echo " vnc port : $vnc_port"
echo " vnc screen : $vnc_screen"
fi
#-----------------------
# /usr/local/src mapping
#-----------------------
if [ "$hostLocal" != "" ] ; then
echo " hostLocal : $hostLocal"
fi
#--------------
# /home mapping
#--------------
if [ "$hostHome" != "" ] ; then
echo " hostHome : $hostHome"
fi
echo ""
exit 0