|
| 1 | +#!/bin/bash -u |
| 2 | + |
| 3 | +# This script is used to run a docker container with graphics support. |
| 4 | +# All arguments to this script except "-c <container_name>" will be appended to a docker run command. |
| 5 | +# If a container name is specified, and this container already exists, the container is re-entered, |
| 6 | +# which easily allows entering the same persistent container from multiple terminals. |
| 7 | +# See documentation for detailed examples: https://moveit.ros.org/install/docker/ |
| 8 | + |
| 9 | +# Example commands: |
| 10 | +# ./gui-docker --rm -it moveit/moveit:foxy-source /bin/bash # Run a (randomly named) container that is removed on exit |
| 11 | +# ./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 |
| 12 | +# ./gui-docker -c container_name # Start (or continue) an interactive bash in a moveit/moveit:foxy-source container |
| 13 | +# ./gui-docker # Same, but use the default container name "default_moveit_container" |
| 14 | + |
| 15 | +function check_nvidia2() { |
| 16 | + # If we don't have an NVIDIA graphics card, bail out |
| 17 | + lspci | grep -qi "vga .*nvidia" || return 1 |
| 18 | + # If we don't have the nvidia runtime, bail out |
| 19 | + if ! docker -D info | grep -qi "runtimes.* nvidia" ; then |
| 20 | + echo "nvidia-docker v2 not installed (see https://github.com/NVIDIA/nvidia-docker/wiki)" |
| 21 | + return 2 |
| 22 | + fi |
| 23 | + echo "found nvidia-docker v2" |
| 24 | + DOCKER_PARAMS="\ |
| 25 | + --runtime=nvidia \ |
| 26 | + --env=NVIDIA_VISIBLE_DEVICES=${NVIDIA_VISIBLE_DEVICES:-all} \ |
| 27 | + --env=NVIDIA_DRIVER_CAPABILITIES=${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}all" |
| 28 | + return 0 |
| 29 | +} |
| 30 | + |
| 31 | +function check_nvidia1() { |
| 32 | + # If we don't have an NVIDIA graphics card, bail out |
| 33 | + lspci | grep -qi "vga .*nvidia" || return 1 |
| 34 | + # Check whether nvidia-docker is available |
| 35 | + if ! which nvidia-docker > /dev/null ; then |
| 36 | + echo "nvidia-docker v1 not installed either" |
| 37 | + return 2 |
| 38 | + fi |
| 39 | + # Check that nvidia-modprobe is installed |
| 40 | + if ! which nvidia-modprobe > /dev/null ; then |
| 41 | + echo "nvidia-docker-plugin requires nvidia-modprobe. Please install it!" |
| 42 | + return 3 |
| 43 | + fi |
| 44 | + # Retrieve device parameters from nvidia-docker-plugin |
| 45 | + if ! DOCKER_PARAMS=$(curl -s http://localhost:3476/docker/cli) ; then |
| 46 | + echo "nvidia-docker-plugin not responding on http://localhost:3476/docker/cli" |
| 47 | + echo "See https://github.com/NVIDIA/nvidia-docker/wiki/Installation-(version-1.0)" |
| 48 | + return 3 |
| 49 | + fi |
| 50 | + echo "found nvidia-docker v1" |
| 51 | + DOCKER_EXECUTABLE=nvidia-docker |
| 52 | +} |
| 53 | + |
| 54 | +function check_dri() { |
| 55 | + # If there is no /dev/dri, bail out |
| 56 | + test -d /dev/dri || return 1 |
| 57 | + DOCKER_PARAMS="--device=/dev/dri --group-add video" |
| 58 | +} |
| 59 | + |
| 60 | +function transfer_x11_permissions() { |
| 61 | + # store X11 access rights in temp file to be passed into docker container |
| 62 | + XAUTH=/tmp/.docker.xauth |
| 63 | + touch $XAUTH |
| 64 | + xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - |
| 65 | +} |
| 66 | + |
| 67 | +function count_positional_args() { |
| 68 | + while true ; do |
| 69 | + case "${1:-}" in |
| 70 | + # Skip common options with a subsequent positional argument |
| 71 | + # This list is not exhaustive! Augment as you see fit. |
| 72 | + -v|--volume) shift ;; |
| 73 | + -w) shift ;; |
| 74 | + -e) shift ;; |
| 75 | + # Skip all other options |
| 76 | + -*) ;; |
| 77 | + *) break ;; |
| 78 | + esac |
| 79 | + shift |
| 80 | + done |
| 81 | + # Return remaining number of arguments |
| 82 | + echo $# |
| 83 | +} |
| 84 | + |
| 85 | +if [ $# -eq 0 ] ; then |
| 86 | + # If no options are specified at all, use the name "default_moveit_container" |
| 87 | + CONTAINER_NAME=default_moveit_container |
| 88 | +else |
| 89 | + # Check for option -c or --container in first position |
| 90 | + case "$1" in |
| 91 | + -c|--container) |
| 92 | + shift |
| 93 | + # If next argument is not an option, use it as the container name |
| 94 | + if [[ "${1:-}" != -* ]] ; then |
| 95 | + CONTAINER_NAME="${1:-}" |
| 96 | + shift |
| 97 | + fi |
| 98 | + # Set default container name if still undefined |
| 99 | + CONTAINER_NAME="${CONTAINER_NAME:-default_moveit_container}" |
| 100 | + ;; |
| 101 | + esac |
| 102 | +fi |
| 103 | + |
| 104 | +transfer_x11_permissions |
| 105 | + |
| 106 | +# Probe for nvidia-docker (version 2 or 1) |
| 107 | +check_nvidia2 || check_nvidia1 || check_dri || echo "No supported graphics card found" |
| 108 | + |
| 109 | +DOCKER_EXECUTABLE=${DOCKER_EXECUTABLE:-docker} |
| 110 | + |
| 111 | +# If CONTAINER_NAME was specified and this container already exists, continue it |
| 112 | +if [ -n "${CONTAINER_NAME:-}" ] ; then |
| 113 | + if [ -z "$($DOCKER_EXECUTABLE ps -aq --filter name=^$CONTAINER_NAME\$)" ] ; then |
| 114 | + # container not yet existing: add an option to name the container when running docker below |
| 115 | + NAME_OPTION="--name=$CONTAINER_NAME" |
| 116 | + if [ "$(count_positional_args $@)" == "0" ] ; then |
| 117 | + # If no further (positional) arguments were provided, start a bash in the default image (for dummy users) |
| 118 | + DUMMY_DEFAULTS="-it moveit/moveit:foxy-source bash" |
| 119 | + fi |
| 120 | + else |
| 121 | + if [ -z "$($DOCKER_EXECUTABLE ps -q --filter name=^$CONTAINER_NAME\$)" ] ; then |
| 122 | + echo -n "Start existing, but stopped container: " |
| 123 | + docker start $CONTAINER_NAME |
| 124 | + fi |
| 125 | + echo "Entering container: $CONTAINER_NAME" |
| 126 | + if [ $# -eq 0 ] ; then |
| 127 | + docker exec -it $CONTAINER_NAME bash |
| 128 | + else |
| 129 | + docker exec $CONTAINER_NAME $@ |
| 130 | + fi |
| 131 | + rm $XAUTH |
| 132 | + exit 0 |
| 133 | + fi |
| 134 | +fi |
| 135 | + |
| 136 | +${DOCKER_EXECUTABLE:-docker} run \ |
| 137 | + --env="DISPLAY=$DISPLAY" \ |
| 138 | + --env="QT_X11_NO_MITSHM=1" \ |
| 139 | + --env="XAUTHORITY=$XAUTH" \ |
| 140 | + --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \ |
| 141 | + --volume="$XAUTH:$XAUTH" \ |
| 142 | + ${NAME_OPTION:-} \ |
| 143 | + ${DOCKER_PARAMS:-} \ |
| 144 | + $@ ${DUMMY_DEFAULTS:-} |
| 145 | + |
| 146 | +# cleanup |
| 147 | +rm $XAUTH |
0 commit comments