-
Notifications
You must be signed in to change notification settings - Fork 0
/
rundocker.sh
executable file
·133 lines (118 loc) · 3.56 KB
/
rundocker.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
#!/bin/sh
CWD=$(cd `dirname $0`; pwd;)
GITHACK=n
MAPPORTS=`expr $UID - 1000`
DOCKER_FILENAME=$CWD/docker/stagedml_dev.docker
DOCKER_IMGNAME=""
DOCKER_COMMAND="bash --login"
while test -n "$1" ; do
case "$1" in
-h|--help)
echo "Usage: $0 [-n|--no-map-ports]"\
"[-m INT|--map-ports-base INT]"\
"(FILE_NAME.docker|DOCKER_IMAGE_NAME)"\
"[-- COMMAND [ARG1 [ARG2..]]]" >&2
exit 1
;;
-n|--no-map-ports)
MAPPORTS=""
;;
-m|--map-ports-base)
MAPPORTS="$2"; shift
;;
--) shift
DOCKER_COMMAND=""
while test -n "$1"; do
DOCKER_COMMAND="$DOCKER_COMMAND $1"
shift
done
break
;;
*)
if test -f "$1" ; then
DOCKER_FILENAME="$1"
else
DOCKER_IMGNAME="$1"
fi
;;
esac
shift
done
# The code snippet below builds and injects nix packages into the Docker
# container, if the specified expression file does exist. It is a matter of
# comfort and is not actually required for system operation.
if which nix-build && test -f "$CWD/nix/docker_inject.nix" ; then
nix-build $CWD/nix/docker_inject.nix --argstr me $USER --out-link "$CWD/.nix_docker_inject.env"
if test "$?" != "0" ; then
echo "nix-build failed, continue without nix injections" >&2
fi
else
echo "No nix-expressions to inject into this container" >&2
fi
# Remap detach key to Ctrl+e,e
DOCKER_CFG="/tmp/docker-stagedml-$UID"
mkdir "$DOCKER_CFG" 2>/dev/null || true
cat >$DOCKER_CFG/config.json <<EOF
{ "detachKeys": "ctrl-e,e" }
EOF
set -e -x
if test -n "$DOCKER_IMGNAME" ; then
docker pull "$DOCKER_IMGNAME:latest"
else
DOCKER_SUFFIX=`echo $DOCKER_FILENAME | sed -n 's@.*_\(.*\)\.docker$@\1@p'`
DOCKER_IMGNAME=stagedml/$DOCKER_SUFFIX
docker build \
--build-arg=http_proxy=$https_proxy \
--build-arg=https_proxy=$https_proxy \
--build-arg=ftp_proxy=$https_proxy \
-t "$DOCKER_IMGNAME" \
-f "$DOCKER_FILENAME" "$CWD/docker"
fi
if test -n "$MAPPORTS"; then
PORT_TENSORBOARD=`expr 6000 + $MAPPORTS`
PORT_JUPYTER=`expr 8000 + $MAPPORTS`
DOCKER_PORT_ARGS="
-p 0.0.0.0:$PORT_TENSORBOARD:6006 -e PORT_TENSORBOARD=$PORT_TENSORBOARD
-p 0.0.0.0:$PORT_JUPYTER:8888 -e PORT_JUPYTER=$PORT_JUPYTER"
fi
# To allow X11 connections from docker
xhost +local: || true
cp "$HOME/.Xauthority" "$CWD/.Xauthority" || true
if which nvidia-docker >/dev/null 2>&1; then
DOCKER_CMD=nvidia-docker
else
DOCKER_CMD=docker
fi
# Mount additional folders inside the container
if test -d "/home/data" ; then
DOCKER_MOUNT_ARGS="$DOCKER_MOUNT_ARGS -v /home/data:/home/data"
fi
if test -d "/nix" ; then
DOCKER_MOUNT_ARGS="$DOCKER_MOUNT_ARGS -v /nix:/nix"
fi
if test -d "/tmp/.X11-unix" ; then
DOCKER_MOUNT_ARGS="$DOCKER_MOUNT_ARGS -v /tmp/.X11-unix:/tmp/.X11-unix"
fi
if test -d "/dev/bus/usb" ; then
DOCKER_MOUNT_ARGS="$DOCKER_MOUNT_ARGS --privileged -v /dev/bus/usb:/dev/bus/usb"
fi
${DOCKER_CMD} --config "$DOCKER_CFG" \
run -it --rm \
--volume "$CWD:/workspace" \
--workdir /workspace \
-m 32g \
-e HOST_PERMS="$(id -u):$(id -g)" \
-e "CI_BUILD_HOME=/workspace" \
-e "CI_BUILD_USER=$(id -u -n)" \
-e "CI_BUILD_UID=$(id -u)" \
-e "CI_BUILD_GROUP=$(id -g -n)" \
-e "CI_BUILD_GID=$(id -g)" \
-e "DISPLAY=$DISPLAY" \
-e "EDITOR=$EDITOR" \
-e "TERM=$TERM" \
${DOCKER_PORT_ARGS} \
${DOCKER_MOUNT_ARGS} \
--cap-add=SYS_PTRACE --security-opt seccomp=unconfined \
--privileged -v /dev/bus/usb:/dev/bus/usb \
"${DOCKER_IMGNAME}" \
bash /install/with_the_same_user.sh $DOCKER_COMMAND