forked from InsightSoftwareConsortium/SimpleITK-Notebooks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·136 lines (117 loc) · 2.89 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
#!/bin/sh
container=simpleitk-notebooks
image=insighttoolkit/simpleitk-notebooks
port=8888
extra_run_args=""
quiet=""
show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-q] [-c CONTAINER] [-i IMAGE] [-p PORT] [-r DOCKER_RUN_FLAGS]
This script is a convenience script to run Docker images. It:
- Makes sure docker is available
- On Windows and Mac OSX, creates a docker machine if required
- 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/jovyan/notebooks on Linux and Mac OSX
- Prints out the graphical app output log following execution
Options:
-h Display this help and exit.
-c Container name to use (default ${container}).
-i Image name (default ${image}).
-p Port to expose the notebook server (default ${port}). If an empty
string, the port is not exposed.
-r Extra arguments to pass to 'docker run'.
-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
;;
-r)
extra_run_args="$extra_run_args $2"
shift
;;
-q)
quiet=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" ]; 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() {
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 notebook container..."
echo ""
if [ -n "$port" ]; then
echo "Point your web browser to ${url}"
echo ""
fi
fi
pwd_dir="$(pwd)"
mount_local=""
if [ "${os}" = "Linux" ] || [ "${os}" = "Darwin" ]; then
mount_local=" -v ${pwd_dir}:/home/jovyan/notebooks "
fi
port_arg=""
if [ -n "$port" ]; then
port_arg="-p $port:8888"
fi
docker run \
-d \
--name $container \
${mount_local} \
$port_arg \
$extra_run_args \
$image >/dev/null
trap "docker stop $container >/dev/null" SIGINT SIGTERM
docker wait $container >/dev/null
# vim: noexpandtab shiftwidth=4 tabstop=4 softtabstop=0