forked from dfki-ric/docker_image_development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_commands.bash
More file actions
executable file
·117 lines (100 loc) · 3.96 KB
/
docker_commands.bash
File metadata and controls
executable file
·117 lines (100 loc) · 3.96 KB
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
#!/bin/bash
. ./settings.bash
DNSIP=$(nmcli dev show | grep 'IP4.DNS' | grep "\[1\]" | egrep -oe "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}" | head -n 1)
init_docker(){
#detect if nvidia runtime is available
RUNTIMES=$(docker info | grep "Runtimes:")
if [[ $RUNTIMES == *"nvidia"* ]]; then
echo "has nvidia runtime"
RUNTIME_ARG="--runtime=nvidia"
else
echo "deprecated nvidia-docker2 hardware acceleration not available, testing newer docker --gpu setting"
RUNTIME_ARG=""
fi
#detect if gpus command is supported and working from docker 19.03
GPUS_SETTINGS_FILE="has_gpu_support.txt"
if [ ! -f $GPUS_SETTINGS_FILE ]; then
touch $GPUS_SETTINGS_FILE
docker run --gpus=all --rm $IMAGE_NAME > /dev/null
GPU_SUPPORTED=$?
echo "this file contains the gegerated result of testing the docker --gpu setting, 0=enabled, 1=disabled" > $GPUS_SETTINGS_FILE
echo $GPU_SUPPORTED >> $GPUS_SETTINGS_FILE
fi
HAS_GPU_SUPPORT=$(tail -n1 $GPUS_SETTINGS_FILE)
if [[ $HAS_GPU_SUPPORT = "0" ]]; then
echo "docker supports --gpu setting, hardware acceleration enabled"
RUNTIME_ARG="--gpus all"
else
echo "hardware acceleration disabled"
fi
if [ "$1" = "noninteractive" ]; then
INTERACTIVE="false"
#remove first param
shift
fi
# check if a container from previous runs exist
if [ "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
#check if the local image is newer that the one the container was crested with
#generate_container saves the curretn id to a file
echo "found existing container"
if [ "$CURRENT_IMAGE_ID" = "$CONTAINER_IMAGE_ID" ]; then
echo "using existent container"
if [ "$INTERACTIVE" = "true" ]; then
start_container $@
else
start_container_nonint $@
fi
else
echo "image id is newer that container image id, removing old container:"
#stop the container in case it is running
docker stop $CONTAINER_NAME
docker rm $CONTAINER_NAME
if [ "$INTERACTIVE" = "true" ]; then
generate_container $@
else
generate_container_nonint $@
fi
fi
else
echo "inital run, no container exists"
if [ "$INTERACTIVE" = "true" ]; then
generate_container $@
else
generate_container_nonint $@
fi
fi
}
#generate container and start command given ad paramater
generate_container(){
echo "generating new container : $CONTAINER_NAME"
echo $CURRENT_IMAGE_ID > $CONTAINER_ID_FILENAME
#initial run exits no matter what due to entrypoint (user id settings)
#/bin/bash will be default nonetheless when called later without command
docker run -ti $RUNTIME_ARG $DOCKER_RUN_ARGS $IMAGE_NAME || exit 1
# default container exists after initial run
echo "docker start $CONTAINER_NAME"
docker start $CONTAINER_NAME
echo "running /opt/check_init_workspace.bash in $CONTAINER_NAME"
docker exec -ti $CONTAINER_NAME /opt/check_init_workspace.bash
echo "running $@ in $CONTAINER_NAME"
docker exec -ti $CONTAINER_NAME $@
}
#starts container with the param given in first run
start_container(){
echo "docker start $CONTAINER_NAME"
docker start $CONTAINER_NAME
echo "running $@ in $CONTAINER_NAME"
docker exec -ti $CONTAINER_NAME $@
}
generate_container_nonint(){
echo "generating new non-interactive container with $@"
echo $CURRENT_IMAGE_ID > $CONTAINER_ID_FILENAME
docker run -t $RUNTIME_ARG $DOCKER_RUN_ARGS $IMAGE_NAME $@
# default container exists after initial run
start_container_nonint $@
}
#starts container with the param given in first run
start_container_nonint(){
echo "docker start non-interactive $CONTAINER_NAME"
docker start -a $CONTAINER_NAME
}