forked from thewtex/docker-opengl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.sh
executable file
·148 lines (129 loc) · 3.19 KB
/
run.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
#!/bin/bash
container=opengl
image=gilureta/opengl:ubuntu16.04
port=6080
extra_run_args=""
extra_docker_args=""
quiet=""
use_vm=""
show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-q] [-m] [-c CONTAINER] [-i IMAGE] [-p PORT] [-r DOCKER_RUN_FLAGS] [-d DOCKER_RUN_ARGS]
This script is a convenience script to run Docker images based on
thewtex/opengl. It:
- Makes sure docker is available
- On Windows and Mac OSX, creates a docker machine if asked (flag -d)
- Informs the user of the URL to access the container with a web browser
- Stops and removes containers from previous runs to avoid conflicts
- Mounts the present working directory to /home/user/work on Linux and Mac OSX
- Prints out the graphical app output log following execution
- Exits with the same return code as the graphical app
Options:
-h Display this help and exit.
-c Container name to use (default ${container}).
-m Run docker virtual machine.
-i Image name (default ${image}).
-p Port to expose HTTP server (default ${port}). If an empty
string, the port is not exposed.
-r Extra arguments to pass to 'docker run'. E.g.
--env="APP=glxgears"
-q Do not output informational messages.
EOF
}
while [ $# -gt 0 ]; do
case "$1" in
-h)
show_help
exit 0
;;
-c)
container=$2
shift
;;
-i)
image=$2
shift
;;
-p)
port=$2
shift
;;
-d)
extra_docker_args="$extra_docker_args $2"
shift
;;
-r)
extra_run_args="$extra_run_args $2"
shift
;;
-q)
quiet=1
;;
-m)
use_vm=1
;;
*)
show_help >&2
exit 1
;;
esac
shift
done
which docker 2>&1 >/dev/null
if [ $? -ne 0 ]; then
echo "Error: the 'docker' command was not found. Please install docker."
exit 1
fi
os=$(uname)
if [ "${os}" != "Linux" ] && [ ! -z $use_vm ]; then
vm=$(docker-machine active 2> /dev/null || echo "default")
if ! docker-machine inspect "${vm}" &> /dev/null; then
if [ -z "$quiet" ]; then
echo "Creating machine ${vm}..."
fi
docker-machine -D create -d virtualbox --virtualbox-memory 2048 ${vm}
fi
docker-machine start ${vm} > /dev/null
eval $(docker-machine env $vm --shell=sh)
fi
ip=$(docker-machine ip ${vm} 2> /dev/null || echo "localhost")
url="http://${ip}:$port"
cleanup() {
echo "cleanup"
docker stop $container >/dev/null
docker rm $container >/dev/null
}
running=$(docker ps -a -q --filter "name=${container}")
if [ -n "$running" ]; then
if [ -z "$quiet" ]; then
echo "Stopping and removing the previous session..."
echo ""
fi
cleanup
fi
if [ -z "$quiet" ]; then
echo ""
echo "Setting up the graphical application container..."
echo ""
if [ -n "$port" ]; then
echo "Point your web browser to ${url}"
echo ""
fi
fi
port_arg=""
if [ -n "$port" ]; then
port_arg="-p $port:6080"
fi
docker run \
-d \
--name $container \
--mount type=bind,source="$(pwd)",target=/workspace \
$port_arg \
$extra_docker_args \
$image \
$extra_run_args
trap "docker stop $container >/dev/null && print_app_output" SIGINT SIGTERM
docker exec -it $container bash
echo "Stopping container $container (it might take a while)"
docker stop $container > /dev/null
cleanup