GUI apps in Docker

When you install a software (say e.g. Tomcat) you need to make sure all its dependencies are satisfied. In the Tomcat example, you’d need to have a JDK, and the JDK version should be coherent with the version of Tomcat you use. Tools like brew are great for managing the installation of software and relative dependencies. However, they do it system-wide. Sometimes however there might be conflicts. For instance, a certain web-app could depend on a recent version of PHP, and another one on an older, incompatible version. How can you solve these conflicts? Containers, like Docker, come to the rescue. They built for you a protected environment in which you can install software and dependencies without doing that in a system-wide fashion.

The concept is applied on linux machines – but you can do that also on Mac or Windows. You need however a special version of docker: Docker desktop in the Mac or Win version. Having installed it, you can use a container, but remember that the version of the software you’re using is a Linux version! So you and up running Tomcat in a protected environment on your machine, but it will be a Linux Tomcat on you Mac (or Win)!

That is fine, but there is a complication if the app in your container uses a GUI. It needs a Linux gui! There is a solution. You need to install X on your machine: for Mac, just go and grab XQuartz. Install it: now you have an X server on your Mac. To be able to allow an app inside a docker container to use it, you need to go through a few simple steps, as described by cschiewek. In particular, you first need (once) to launch XQuartz, select Preferences, go to the security tab and ensure “Allow connections from network clients” is checked. At this point is essential that you restart XQuartz! (this step was not explicitly mentioned by cschiewek). Now your X server is ready to offer its services to apps within docker.

As an example, let’s consider a container that encapsulates Firefox:

To launch it, first let’s make sure that XQuartz is runnung, then, in a shell, let’s give the following commands:

export IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}') 

docker run --rm  --name firefox1 -e DISPLAY=$IP:0 -e XAUTHORITY=/.Xauthority --net host -v /tmp/.X11-unix:/tmp/.X11-unix -v ~/.Xauthority:/.Xauthority  jess/firefox

At this point (after the image has been pulled and started) you should see firefox running in an X window. Be aware, however, that XQuartz will not reproduce audio (or at least I haven’t found a solution for this issue)- so if you open youtube in your dockerized firefox, it will be mute.

BTW, if you are not familiar with Docker and the concept of container, there are a lot of tutorials on youtube. This is one I liked.

And, if you want to get a deeper understand of what a container is and how it work, take a look at this absolutely great talk. a MUST!

Posted in Docker