How can you run GUI applications in a Docker container?
Are there any images that set up vncserver
or something so that you can - for example - add an extra speedbump sandbox around say Firefox?
How can you run GUI applications in a Docker container?
Are there any images that set up vncserver
or something so that you can - for example - add an extra speedbump sandbox around say Firefox?
For OpenGL rendering with the Nvidia driver, use the following image:
https://github.com/thewtex/docker-opengl-nvidia
For other OpenGL implementations, make sure the image has the same implementation as the host.
I'm late to the party, but for Mac users who don't want to go down the XQuartz path, here is a working example that builds a Fedora Image, with a Desktop Environment (xfce) using
Xvfb
andVNC
. It's simple, and works:On a Mac, you can just access it using the Screen Sharing (default) application, connecting to
localhost:5901
.Dockerfile:
start-vnc.sh
Check the linked readme for build and run commands if you want/need.
Xauthority becomes an issue with newer systems. I can either discard any protection with xhost + before running my docker containers, or I can pass in a well prepared Xauthority file. Typical Xauthority files are hostname specific. With docker, each container can have a different host name (set with docker run -h), but even setting the hostname of the container identical to the host system did not help in my case. xeyes (I like this example) simply would ignore the magic cookie and pass no credentials to the server. Hence we get an error message 'No protocol specified Cannot open display'
The Xauthority file can be written in a way so that the hostname does not matter. We need to set the Authentication Family to 'FamilyWild'. I am not sure, if xauth has a proper command line for this, so here is an example that combines xauth and sed to do that. We need to change the first 16 bits of the nlist output. The value of FamilyWild is 65535 or 0xffff.
Based on Jürgen Weigert's answer, I have some improvement:
The only difference is that it creates a directory $XAUTH_DIR which is used to place $XAUTH file and mount $XAUTH_DIR directory instead of $XAUTH file into docker container.
The benefit of this method is that you can write a command in /etc/rc.local which is to create a empty folder named $XAUTH_DIR in /tmp and change its mode to 777.
When system restart, before user login, docker will mount the $XAUTH_DIR directory automatically if container's restart policy is "always". After user login, you can write a command in ~/.profile which is to create $XAUTH file, then the container will automatically use this $XAUTH file.
Afterall, the container will automatically get the Xauthority file every time the system restart and user login.
You can also use subuser: https://github.com/timthelion/subuser
This allows you to package many gui apps in docker. Firefox and emacs have been tested so far. With firefox, webGL doesn't work though. Chromium doesn't work at all.
EDIT: Sound works!
EDIT2: In the time since I first posted this, subuser has progressed greatly. I now have a website up subuser.org, and a new security model for connecting to X11 via XPRA bridging.
Here's a lightweight solution that avoids having to install any
X
server,vnc
server orsshd
daemon on the container. What it gains in simplicity it loses in security and isolation.It assumes that you connect to the host machine using
ssh
withX11
forwarding.In the
sshd
configuration of the host, add the lineSo that the forwarded X server port on the host is opened on all interfaces (not just
lo
) and in particular on the Docker virtual interface,docker0
.The container, when run, needs access to the
.Xauthority
file so that it can connect to the server. In order to do that, we define a read-only volume pointing to the home directory on the host (maybe not a wise idea!) and also set theXAUTHORITY
variable accordingly.That is not enough, we also have to pass the DISPLAY variable from the host, but substituting the hostname by the ip:
We can define an alias:
And test it like this: