Skip to content

Instantly share code, notes, and snippets.

@danielTobon43
Last active May 31, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielTobon43/c3590b23fe076eaf41fcead99f69fadf to your computer and use it in GitHub Desktop.
Save danielTobon43/c3590b23fe076eaf41fcead99f69fadf to your computer and use it in GitHub Desktop.
Docker display GUI

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:

Option 1

# 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.

Option 2

#!/bin/bash
#
set -x
export DISPLAY=:99.0
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
sleep 3
set +x
exec "$@"

Option 3

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

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment