How to enable GLX support when using Nvidia-docker2

In order to use Gazebo in Docker which needs GLX render function, I need to use nvidia-docker 1 and 2.

nvidia-docker is weired, they support glx in nvidia-docker 1.0 but not in nvidia-docker 2.0 (and they don’t have schedule to implement it).

1.0 2.0 coexistence

According offical Wiki for Nvidia-docker, nvidia-docker 2 is consisted by two packages, nvidia-docker2 and nvidia-runtime-container. Actually, nvidia-docker 2 is just a warper. You just need nvidia-runtime-container, by register it manually to daemon.json. And run with nvidia 2 by docker run --runtime="nvidia" blabla.

And then install nvidia-docker 1.0 by deb info.

Create Image

There are no difference when creating image with 1.0 or 2.0, So create Image with:

FROM nvidia/cuda

run nvidia-docker 1.0 then call glxgears

For nvidia-docker 1.0, you must need nvidia-docker instead of docker --runtime=xx. Here is my script:

#!/bin/zsh

HOMEDIR=/home/sxs/DRL/docker/dockerws/
WORKSPACE=/home/sxs/DRL/docker/marioai
xhost +local:root
nvidia-docker run -it --rm \
    --privileged \
    --env="DISPLAY" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --volume="/usr/lib/x86_64-linux-gnu/libXv.so.1:/usr/lib/x86_64-linux-gnu/libXv.so.1" \
    --volume="$HOMEDIR:/home/dock:rw" \
    --volume="$WORKSPACE:/home/dock/workspace:rw" \
    -u 1000 \
    --workdir "/home/dock/workspace/" \
    --network=host \
    sxs/cuda-ros /bin/zsh

xhost -local:root

After boot, you can try glxgears, you should see spin gears then.
Notices: —privileged is required, or there is only black window showed.

Run nvidia-docker 2.0

#!/bin/zsh

HOMEDIR=/home/sxs/DRL/docker/dockerws/
WORKSPACE=/home/sxs/DRL/docker/marioai
xhost +local:root
docker run -it --rm \
    --privileged \
    --runtime="nvidia" \
    --env="DISPLAY" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --volume="/usr/lib/x86_64-linux-gnu/libXv.so.1:/usr/lib/x86_64-linux-gnu/libXv.so.1" \
    --volume="$HOMEDIR:/home/dock:rw" \
    --volume="$WORKSPACE:/home/dock/workspace:rw" \
    -u 1000 \
    --workdir "/home/dock/workspace/" \
    --network=host \
    sxs/cuda-ros /bin/zsh

xhost -local:root