The solution to a general problem is simpler than a solution to a specific problem, the solution of many problem is simpler than the solution to one problem.

Sunday, October 30, 2016

How to Run X Windows Server inside Docker Container

Background

Sometimes I need to run X Windows-based applications inside Docker containers, and running the server locally is too unpractical because of latency reasons or the working laptop has no X Windows Server. First I tried to create a VirtualBox-based Vnc Server, and it worked fine albeit a little slow, but Docker containers seem to have better memory and disk footprint. So I tried to create Vnc Server running X Windows inside a Docker container. I already tried suchja/x11server (ref) but it has strange problems ignoring cursor keys of my MacBook on webkit page (such as Pentaho Data Integration's Formula page).

After comparing the container condition to my VirtualBox VM (that already worked), the VM has downloaded xfonts-100dpi because xfce4 recommends xorg and it requires xfonts-100dpi. The Dockerfile apt line has standard no-install-recommends clause in order to keep images small. So first step is to change the Dockerfile into :

After doing the change, next problem occurred :

Setting up libfontconfig1:amd64 (2.11.0-6.3+deb8u1) ...
Setting up fontconfig (2.11.0-6.3+deb8u1) ...
Regenerating fonts cache... done.
Setting up keyboard-configuration (1.123) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Configuring keyboard-configuration
----------------------------------
Please select the layout matching the keyboard for this machine.
1. English (US)
2. English (US) - Cherokee
3. English (US) - English (Colemak)
4. English (US) - English (Dvorak alternative international no dead keys)
5. English (US) - English (Dvorak)
6. English (US) - English (Dvorak, international with dead keys)
7. English (US) - English (Macintosh)
8. English (US) - English (US, alternative international)
9. English (US) - English (US, international with dead keys)
10. English (US) - English (US, with euro on 5)
11. English (US) - English (Workman)
12. English (US) - English (Workman, international with dead keys)
13. English (US) - English (classic Dvorak)
14. English (US) - English (international AltGr dead keys)
15. English (US) - English (left handed Dvorak)
16. English (US) - English (programmer Dvorak)
17. English (US) - English (right handed Dvorak)
18. English (US) - English (the divide/multiply keys toggle the layout)
19. English (US) - Russian (US, phonetic)
20. English (US) - Serbo-Croatian (US)
21. Other
Keyboard layout:

During image build, there is a prompt asking us about keyboard layout, and typing an answer results in hang process. This error is similar to this stackoverflow question. I tried to do the suggestions there (copying to /etc/default/keyboard), but it still hangs. After struggling with many experiments, finally I use this Dockerfile :

How to use vnc server container

First, you build the image:

docker-compose build

Then, create the containers :

docker-compose up -d

Check the IP of the container (replace vncserver1 with container_name in your docker-compose.yml)

docker inspect vncserver1 | grep 172

Tunnel VNC by performing ssh port forwarding to the local IP (example, 172.17.0.9) and port 5903

ssh -L 5903:172.17.0.9:5903 user@servername

Now to view the screen, you could connect to the VNC server by using VNC Viewer or opening vnc://localhost:5903 url in Safari.

For the X Windows based application, you first must grab the magic cookie :

Variations

This Dockerfile uses LXDE :

This one uses OpenBox Window Manager only :

Conclusion

We are able to run X Window Server running inside a Docker container. The resulting images are about 625 MB, which could be a lot smaller if we remove firefox (iceweasel) and use only Openbox Window Manager.