-
Notifications
You must be signed in to change notification settings - Fork 41
/
gui-docker
executable file
·152 lines (137 loc) · 5.44 KB
/
gui-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
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
149
150
151
152
#!/bin/bash -u
# This script is used to run a docker container with graphics support.
# All arguments to this script except "-c <container_name>" will be appended to a docker run command.
# If a container name is specified, and this container already exists, the container is re-entered,
# which easily allows entering the same persistent container from multiple terminals.
# See documentation for detailed examples: https://moveit.ros.org/install/docker/
# Example commands:
# ./gui-docker --rm -it moveit/moveit:foxy-source /bin/bash # Run a (randomly named) container that is removed on exit
# ./gui-docker -v ~/ros_ws:/root/ros_ws --rm -it moveit/moveit:foxy-source /bin/bash # Same, but also link host volume ~/ros_ws to /root/ros_ws in the container
# ./gui-docker -c container_name # Start (or continue) an interactive bash in a moveit/moveit2:foxy-source container
# ./gui-docker # Same, but use the default container name "default_moveit_container"
function check_nvidia2() {
# If we don't have an NVIDIA graphics card, bail out
if lspci | grep -i "nvidia" && lspci | grep -q nvidia; then
return 1
fi
# If we don't have the nvidia runtime, bail out
if ! docker -D info | grep -qi nvidia ; then
echo "nvidia-docker v2 not installed (see https://github.com/NVIDIA/nvidia-docker/wiki)"
return 2
fi
echo "found nvidia-docker v2"
DOCKER_PARAMS="\
--runtime=nvidia \
--env=NVIDIA_VISIBLE_DEVICES=${NVIDIA_VISIBLE_DEVICES:-all} \
--env=NVIDIA_DRIVER_CAPABILITIES=${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}all"
return 0
}
function check_nvidia1() {
# If we don't have an NVIDIA graphics card, bail out
if lspci | grep -i "nvidia" && lspci | grep -i "vga .*nvidia"; then
return 1
fi
# Check whether nvidia-docker is available
if ! which nvidia-docker > /dev/null ; then
echo "nvidia-docker v1 not installed either"
return 2
fi
# Check that nvidia-modprobe is installed
if ! which nvidia-modprobe > /dev/null ; then
echo "nvidia-docker-plugin requires nvidia-modprobe. Please install it!"
return 3
fi
# Retrieve device parameters from nvidia-docker-plugin
if ! DOCKER_PARAMS=$(curl -s http://localhost:3476/docker/cli) ; then
echo "nvidia-docker-plugin not responding on http://localhost:3476/docker/cli"
echo "See https://github.com/NVIDIA/nvidia-docker/wiki/Installation-(version-1.0)"
return 3
fi
echo "found nvidia-docker v1"
DOCKER_EXECUTABLE=nvidia-docker
}
function check_dri() {
# If there is no /dev/dri, bail out
test -d /dev/dri || return 1
DOCKER_PARAMS="--device=/dev/dri --group-add video"
}
function transfer_x11_permissions() {
# store X11 access rights in temp file to be passed into docker container
XAUTH=/tmp/.docker.xauth
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
}
function count_positional_args() {
while true ; do
case "${1:-}" in
# Skip common options with a subsequent positional argument
# This list is not exhaustive! Augment as you see fit.
-v|--volume) shift ;;
-w) shift ;;
-e) shift ;;
# Skip all other options
-*) ;;
*) break ;;
esac
shift
done
# Return remaining number of arguments
echo $#
}
if [ $# -eq 0 ] ; then
# If no options are specified at all, use the name "default_test"
CONTAINER_NAME=default_test
else
# Check for option -c or --container in first position
case "$1" in
-c|--container)
shift
# If next argument is not an option, use it as the container name
if [[ "${1:-}" != -* ]] ; then
CONTAINER_NAME="${1:-}"
shift
fi
# Set default container name if still undefined
CONTAINER_NAME="${CONTAINER_NAME:-default_test}"
;;
esac
fi
transfer_x11_permissions
# Probe for nvidia-docker (version 2 or 1)
check_nvidia2 || check_nvidia1 || check_dri || echo "No supported graphics card found"
DOCKER_EXECUTABLE=${DOCKER_EXECUTABLE:-docker}
# If CONTAINER_NAME was specified and this container already exists, continue it
if [ -n "${CONTAINER_NAME:-}" ] ; then
if [ -z "$($DOCKER_EXECUTABLE ps -aq --filter name=^$CONTAINER_NAME\$)" ] ; then
# container not yet existing: add an option to name the container when running docker below
NAME_OPTION="--name=$CONTAINER_NAME"
if [ "$(count_positional_args $@)" == "0" ] ; then
# If no further (positional) arguments were provided, start a bash in the default image (for dummy users)
DUMMY_DEFAULTS="-it moveit/moveit2:foxy-source bash"
fi
else
if [ -z "$($DOCKER_EXECUTABLE ps -q --filter name=^$CONTAINER_NAME\$)" ] ; then
echo -n "Start existing, but stopped container: "
docker start $CONTAINER_NAME
fi
echo "Entering container: $CONTAINER_NAME"
if [ $# -eq 0 ] ; then
docker exec -it $CONTAINER_NAME bash
else
docker exec $CONTAINER_NAME $@
fi
rm $XAUTH
exit 0
fi
fi
${DOCKER_EXECUTABLE:-docker} run \
--env="DISPLAY=$DISPLAY" \
--env="QT_X11_NO_MITSHM=1" \
--env="XAUTHORITY=$XAUTH" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
--volume="$XAUTH:$XAUTH" \
${NAME_OPTION:-} \
${DOCKER_PARAMS:-} \
$@ ${DUMMY_DEFAULTS:-}
# cleanup
sudo rm -r $XAUTH