-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_docker
executable file
·106 lines (91 loc) · 2.07 KB
/
run_docker
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
#!/usr/bin/env bash
set -e
usage() {
echo "Usage: $0 [-w <working directory>] -d <docker image> <command> [<args>...]";
echo;
echo " -w The working directory for <command>, defaults to current";
echo " directory.";
echo;
echo " -d Docker image to use to execute <command>. The docker container";
echo " will have";
echo " /groups";
echo " /media/X";
echo " available.";
echo;
echo " -p Ports to expose, see 'man docker run'.";
echo " -e Environmental variable.";
}
# defaults
WORK_DIR=$(pwd)
DOCKER_IMAGE=''
PORTS=''
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# parse command line args
num_args=0
while getopts ":w:d:p:e:" opt; do
case "${opt}" in
w)
WORK_DIR=${OPTARG}
((num_args+=2))
;;
d)
DOCKER_IMAGE=${OPTARG}
((num_args+=2))
;;
p)
PORTS="-p ${OPTARG}"
((num_args+=2))
;;
e)
ENVI="-e ${OPTARG}"
((num_args+=2))
;;
*)
usage
exit
;;
esac
done
shift ${num_args}
COMMAND="$@"
if [ "$DOCKER_IMAGE" == "" ];
then \
echo No docker image provided!
echo
usage
exit
fi
if [ "${COMMAND}" == "" ];
then \
echo No command provided!
echo
usage
exit
fi
CONTAINER_NAME=${CONTAINER_NAME:-${USER}-$(date +%y-%m-%d_%H-%M-%S)-${RANDOM}}
# make sure we have the latest
nvidia-docker pull ${DOCKER_IMAGE}
teardown() {
trap - SIGINT SIGTERM
echo "run_docker: Stopping container ${CONTAINER_NAME}, killing after 5s..."
docker stop -t5 ${CONTAINER_NAME}
echo "run_docker: Container ${CONTAINER_NAME} stopped."
}
trap teardown SIGINT SIGTERM
echo "Running docker as user:group `id -u $USER`:`id -g $USER`"
# run nvidia-docker in background
export NV_GPU=$CUDA_VISIBLE_DEVICES
nvidia-docker \
run \
--rm \
--cgroup-parent=$(cat /proc/self/cpuset) \
--name ${CONTAINER_NAME} \
-e HOME="/home/" \
-v /groups:/groups:shared \
-v /media/X:/media/X:shared \
-w ${WORK_DIR} \
${ENVI} \
${PORTS} \
${DOCKER_IMAGE} \
${COMMAND} &
wait