-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_run.sh
118 lines (106 loc) · 3.92 KB
/
docker_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
#!/usr/bin/env bash
# this script is from dustynv/jetson-nano-containers
CONTAINER_IMAGE="nvcr.io/nvidian/nvidia-l4t-base:r32.4"
USER_VOLUME=""
USER_COMMAND=""
show_help() {
echo " "
echo "usage: Starts the Docker container and runs a user-specified command"
echo " "
echo " ./scripts/docker_run.sh --container DOCKER_IMAGE"
echo " --volume HOST_DIR:MOUNT_DIR"
echo " --run RUN_COMMAND"
echo " "
echo "args:"
echo " "
echo " --help Show this help text and quit"
echo " "
echo " -c, --container DOCKER_IMAGE Specifies the name of the Docker container"
echo " image to use (default: 'nvidia-l4t-base')"
echo " "
echo " -v, --volume HOST_DIR:MOUNT_DIR Mount a path from the host system into"
echo " the container. Should be specified as:"
echo " "
echo " -v /my/host/path:/my/container/path"
echo " "
echo " -r, --run RUN_COMMAND Command to run once the container is started."
echo " Note that this argument must be invoked last,"
echo " as all further arguments will form the command."
echo " If no run command is specified, an interactive"
echo " terminal into the container will be provided."
echo " "
}
die() {
printf '%s\n' "$1"
show_help
exit 1
}
# parse arguments
while :; do
case $1 in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit
;;
-c|--container) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
CONTAINER_IMAGE=$2
shift
else
die 'ERROR: "--container" requires a non-empty option argument.'
fi
;;
--container=?*)
CONTAINER_IMAGE=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
--container=) # Handle the case of an empty --image=
die 'ERROR: "--container" requires a non-empty option argument.'
;;
-v|--volume)
if [ "$2" ]; then
USER_VOLUME=" -v $2 "
shift
else
die 'ERROR: "--volume" requires a non-empty option argument.'
fi
;;
--volume=?*)
USER_VOLUME=" -v ${1#*=} " # Delete everything up to "=" and assign the remainder.
;;
--volume=) # Handle the case of an empty --image=
die 'ERROR: "--volume" requires a non-empty option argument.'
;;
-r|--run)
if [ "$2" ]; then
shift
USER_COMMAND=" $@ "
else
die 'ERROR: "--run" requires a non-empty option argument.'
fi
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
#echo "CONTAINER_IMAGE: $CONTAINER_IMAGE"
#echo "USER_VOLUME: $USER_VOLUME"
#echo "USER_COMMAND: '$USER_COMMAND'"
# give docker root user X11 permissions
sudo xhost +si:localuser:root
# enable SSH X11 forwarding inside container (https://stackoverflow.com/q/48235040)
XAUTH=/tmp/.docker.xauth
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
chmod 777 $XAUTH
# run the container
sudo docker run --runtime nvidia -it --rm --privilieged --network host -e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix/:/tmp/.X11-unix \
-v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH \
$USER_VOLUME $CONTAINER_IMAGE $USER_COMMAND