Skip to content

Instantly share code, notes, and snippets.

@armsp
Created December 12, 2018 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save armsp/b664919ea151dbb9589225bcc1393768 to your computer and use it in GitHub Desktop.
Save armsp/b664919ea151dbb9589225bcc1393768 to your computer and use it in GitHub Desktop.
Gist containing simple Dockerfile and steps to run GUI from a docker container
#FROM python:3.7 ## Works with either of the base images. Size is larger with Python due to there being python2.7 too
FROM ubuntu:18.10
RUN apt-get update && apt-get upgrade -y
RUN apt-get install python3-dev python3-pip -y
RUN apt-get install python3-gi -y && \
apt-get install gir1.2-gtk-3.0 -y && \
apt-get install gir1.2-appindicator3-0.1 -y
RUN apt-get install python3-gi-cairo -y
RUN apt install libgirepository1.0-dev -y #libcanberra-gtk3-module
RUN pip3 install pygobject
# Any GUI app script of your choice
COPY switch.py appindicator.py /
CMD ["python3", "switch.py"]

COMMANDS to run the contianer and see the GUI pop out of it

  • sudo docker run --rm -it --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" 0e72e15e1897 after xhost local:root
  • sudo docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix f7c377161291 also works after xhost local:root

OR the latest and the correct way to do this is as follows -

  1. Add these two lines to your Dockerfile:

    • RUN apt-get install -qqy x11-apps
    • ENV DISPLAY $DISPLAY
  2. then run the container as follows -
    xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f /tmp/.docker.xauth nmerge - && docker run -it -v /tmp/.X11-unix:/tmp/.X11-unix -v /tmp/.docker.xauth:/tmp/.docker.xauth -e XAUTHORITY=/tmp/.docker.xauth -e DISPLAY=$DISPLAY <docker image name>

Or you can shorten it and run it like this too -

  • XSOCK=/tmp/.X11-unix
  • XAUTH=/tmp/.docker.xauth
  • xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
  • sudo docker run --rm -ti -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH -e DISPLAY=$DISPLAY <docker image name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment