CODEX
Running GUI Applications in Docker — Firefox, Nautilus File Manager.
What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that the machine might have that could differ from the machine used for writing and testing the code.
In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
Types of applications (usually services) that you can containerize:
- Applications that run as a Background Service (like a Database, WebServer, etc)
- GUI Applications that run in the foreground.
Today, we are going to see the second option:
X Window System/ X:
X11 is the current version of the X Window System.
X provides the basic framework for a GUI environment: drawing and moving windows on the display device and interacting with a mouse and keyboard. X does not mandate the user interface — this is handled by individual programs. As such, the visual styling of X-based environments varies greatly; different programs may present radically different interfaces.on Unix-like operating systems.
For a GUI Application to run, we need to have an XServer which is available as part of every Linux Desktop Environment.
Run the following command to see the X Server and X Client running on your Linux Machine:
$ ps -aux | grep X
For Windows, you can use Xming.
For OSX, you can use Xquarkz.
But within a Container, we don’t have any XServer — so we will:
- Mount the
X11
socket residing in/tmp/.X11-unix
on your local machine into/tmp/.X11-unix
in the container.
-v /tmp/.X11-unix:/tmp/.X11-unix
- share the Host’s DISPLAY environment variable to the Container
-e DISPLAY=$DISPLAY
- run container with host network driver with
--net=host
Example 1: Dockerfile to build an image with a sample GUI application (Firefox):
$ cat Dockerfile
$ sudo docker build -t mtabishk/firefox .
Docker Hub: https://hub.docker.com/repository/docker/mtabishk/firefox
$ sudo docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --net=host mtabishk/firefox:v1.0.0
You should see the firefox GUI application now displayed on your Host OS Desktop.
Example 2: Dockerfile to build an image with a sample GUI application (Nautilus File Manager):
$ cat Dockerfile
$ sudo docker build -t mtabishk/nautilus-ubuntu .
Docker Hub: https://hub.docker.com/repository/docker/mtabishk/nautilus-ubuntu
$ sudo docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix mtabishk/nautilus-ubuntu
You should see the Nautilus GUI application now displayed on your Host OS Desktop.
That’s all for today! I’ll be back with some new articles very soon, thanks! 🤗
Muhammad Tabish Khanday
LinkedIn: https://www.linkedin.com/in/mtabishk/