GUI Apps and Desktop Environment on Docker

After reading this blog, you will be able to understand how the windowing system works in *nix systems and be able to launch GUI applications in remote systems that actually forward their display to your local computer. And we will use that same trick to launch GUI applications and Desktop environments on top of docker.

First things first, how exactly does the display/GUI/windowing system work in *nix systems?

X windowing system

X windowing system is also known as simply X or X11. This is actually a server-client architecture where the clients are the GUI applications that send data to the X server. The X server accepts inputs from an input device like keyboard or mouse and depending on the currently running X client(a GUI application), it sends this data to the kernel for further processing. Then the output is returned to the X server which it renders on to the screen.

The basic architecture of a display in a Linux system consists of a graphical interface/user interface, display server and window manager. The user interface is also referred to as a display manager or simply a desktop environment. This is essentially the whole desktop that the user interacts with! I’m sure if you are a Linux user then you might have heard of Desktop Environments like Gnome, KDE plasma, … Next, coming to the display server which in our case is the X server that directly interacts with the kernel to process the data and display it to the user. And then there’s also the window manager which is responsible for managing the state of windows including resizing, moving around, … Some examples are Compiz and OpenBox.

With this server-client architecture, it is even possible to forward the display to a remote X server which is exactly what we’ll be doing in this tutorial! This X11 forwarding connection between the local system and the remote system (virtual machine in our case) is secured via SSH connection.

Practical

I’ll be doing this practical with my base OS as windows and RHEL 8 on VMWare. First, we need to download VcXsrv. You can download it from here.

Installation is pretty straight forward:

Then run the XLaunch application which will be added to the start menu and choose the following configuration:

Check the “Disable Access Control”
Allow access here

With this X server is ready and you’ll be able to see an “X” icon in the system tray.

Now it's time to set up an X client in a VM. For this, I’ll be using Gedit. For those who don’t know what it is, it's a popular GUI text editor in Linux. All we have to do is run the below commands.

yum install gedit -yexport DISPLAY=<IP>:0.0gedit

Note: Here enter the IP of the system on which the X server is running.

And viola here we go!

Gedit on Docker

Now that we’ve done this in a VM we can do the same in docker by either manually following the above commands or by using a Dockerfile, I’ll be doing it with the Dockerfile.

FROM centosRUN yum install gedit -yCMD ['/usr/bin/gedit']

Now we have to build the image

docker build . -t <IMAGE_NAME>:<IMAGE_TAG>

If you find it too bothersome to make the image you can directly use my image on DockerHub syogesh174/gedit:1

Once the image is built, use the below command to launch a container, also don’t forget to mention the DISPLAY environment variable here.

docker run -dit --rm -e DISPLAY=<IP>:0.0  syogesh174/gedit:1

To store the data persistently and also to load the configuration files of Gedit you can do the following:

PS: You can launch any GUI application, it is not limited to Gedit.

Gnome Desktop Environment on Docker

Till now what we have done was only launching a GUI application in a docker container. In addition to this, we can also launch an entire desktop environment and forward it to the X server in windows. There are some tools that achieve this job easily. One famous tool is x11docker, you can know more about it here. I will just demonstrate the Gnome desktop environment in this blog but you can check out the others.

First, download the x11docker using the below command

curl -fsSL https://raw.githubusercontent.com/mviereck/x11docker/master/x11docker | sudo bash -s -- --update

Then, pull the docker image which will provide us with the Gnome desktop environment.

docker pull x11docker/gnome

And finally, export the DISPLAY environment variable in the VM and launch the docker container from the image.

export DISPLAY=192.168.99.1:0.0x11docker --init=systemd  x11docker/gnome

But before you do this close the X server and launch it again in Full-Screen mode like shown below with the previous configurations above.

Note: If you are stuck with a black screen just do Alt + Tab

Viola, you’ve now got a Gnome desktop environment in a container!!

Note: Make sure that you SSH into the remote system with X11 Forwarding, by using -X option (uppercase) of SSH.

For a more detailed explanation on Xservers and Desktop Environments do refer to this. And to know more about launching different desktop environments on docker for various purposes check this out.

Thank you!