Running GUI applications inside a Docker container

Can you run a GUI application in Docker?

The answer is Yes!!

While the IT world is embracing Containers Technology primarily for Enterprise Server Applications, There is also a huge scope of Docker Containers impacting the Desktop and Development Environment. We leverage this heavily at Admatic, where we containerise every application and service we use — further including the GUI applications and tools that we use for Day to day Development — what took us days is now just a second away. :)

Thanks to Docker Containers that is revolutionising the current DevOps landscape and we plan to revolutionise the current development environment landscape with the same.

Here will share you how to Containerise a GUI app in Docker

There can be two types of applications (usually services) that you can containerise,

  • Applications that run as a Background Service (like a Database, WebServer, etc)
  • GUI Applications that (obviously!) run in the foreground

The second option is what we will see now,

Let’s say you are trying to build a UI application and deploying it as a Docker Container. If you want that UI application to display the user interface on your local machine while running the application inside the Docker Container, you will have to connect the display of the Docker Container with the display of your local machine. Let’s suppose you want to run Mozilla Firefox inside a Docker Container but you want to access the Firefox browser on your local machine. You can do this using some simple steps that we are going to discuss in this article.

Skimming through the entire process, you need to forward the X11 socket of your local Linux machine to the Docker Container for it to use directly. Along with this, you also need to forward the environment variable called display. After that, you need to set the permissions for the server host. Let’s run through these steps along with the code.

1. Creating the dockerfile

Create a dockerfile with the following code.

FROM centos:latestRUN yum update -y
RUN yum install firefox -y
RUN yum install gedit -y
RUN yum install xauth -y
EXPOSE 8687CMD /usr/bin/firefox

he above dockerfile contains the sequence of instructions to create an Centos base image, runs an apt update on it, installs Xauth and Firefox. It then exposes port 8687 and runs Firefox. Xauth simply allows Docker Containers to access the Display Servers.

2. Copying the Cookie to connect X Server Displays

On your local machine, get the cookie value using the following command.

xauth list

We need to create Authority file first then create cookie using command

#Create a file first for cookie
touch ~/.Xauthority
#Create cookie for Authority
xauth add ${1-1051}:0 . $(xxd -l 16 -p /dev/urandom)

After running the above command then check again with “xauth list”

Authorization Cookie

Copy the output which would be of the form as shown below:

<username>/unix: MIT-MAGIC-COOKIE-1 f1fa006c1e51fa8386209f85c57947c4

3. Build the Docker Image

Create the Docker Image using the following command.

docker build -t <image-name>:<version> .

4. Run the Docker Container

Run the Docker Image using the Docker run command.

docker run -it --net=host -e DISPLAY -v /tmp/.X11-unix <image-name> bash

The Docker image is now built and the Container is started. It pops up an interactive centos bash.

5. Add the cookie to the list

You need to add the cookie copied in the previous step using the following command.

xauth add <cookie>
xauth list
Adding Cookie

The list command will verify that the cookie has been added successfully.

6. Run the Firefox Instance from the bash

To run a Firefox instance, simply type “firefox” inside the bash. You will find that the Firefox browser pops up on your local machine even though it is running inside the Docker Container. Using a similar process, you can run almost any user interface application inside Docker Containers and access it on your local machine.

Running Firefox Instance

To conclude, in this article we saw how to create a Docker Image using the dockerfile, build and run the image. We also discussed how to connect the display of the Docker Container to the display of the local machine and accessed a UI application running inside Container on the local machine.

yum doesn’t have Internet connectivity in the container please do the following
# Masquerading allows for docker ingress and egress

firewall-cmd --zone=public --add-masquerade --permanent

# Specifically allow incoming traffic on port 80/443 (nothing new here)

firewall-cmd --zone=public --add-port=80/tcp
firewall-cmd --zone=public --add-port=443/tcp

# Reload firewall to apply permanent rules

firewall-cmd --reload

#Restart docker Services

systemctl restart docker

Thanks For Reading❗❗

Arth Learner — LinuxWorld Informatics Pvt Ltd