Displaying a GUI based application in Docker is typically a moment of stackoverflow search, for the sake of time, and for the reckless lazy ones, the easiest solution is to give xhost permission and then remove the permission.
A possible error when displaying things inside a docker container will go along the lines of:
ERROR: In /build/vtk6-VHOYAG/vtk6-6.3.0+dfsg1/Rendering/OpenGL/vtkXOpenGLRenderWindow.cxx, line 1475
vtkXOpenGLRenderWindow (0x563fbe453620): bad X server connection. DISPLAY=:1. Aborting.
This happens when calling:
docker run -it --rm --env="DISPLAY" --env="QT_X11_NO_MITSHM=1" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" spark_vio
where spark_vio
is my app in this case.
A quick solution is to run instead:
# Allow X server connection
xhost +local:root
docker run -it --rm \
--env="DISPLAY" \
--env="QT_X11_NO_MITSHM=1" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
spark_vio
# Disallow X server connection
xhost -local:root
Mind that the last line is important or you’ll be leaving the door open for malicious scripts to display things on your screen or, worst, capture input!
Happy coding.
#!/bin/bash
#
set -x
export DISPLAY=:99.0
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
sleep 3
set +x
exec "$@"
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
CONTAINER_USER=pcl
docker run -it \
--gpus all \
--volume=$XSOCK:$XSOCK:rw \
--volume=$XAUTH:$XAUTH:rw \
--env="XAUTHORITY=${XAUTH}" \
--env="DISPLAY" \
--name="pcl-docker" \
--cap-add sys_ptrace \
-p 127.0.0.1:2222:22 \
--user=$CONTAINER_USER \
--volume=`pwd`/example_project:/home/$CONTAINER_USER/project \
dbscan:2.0