Docker Display GUI with X server

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.