Running GUI Applications on Docker in Linux

By SOURABH MISHRA

Sourabh Mishra
3 min readApr 23, 2021

There’s an use case in which you want to build a UI application and deploying it as a Docker Container. By default Docker don’t give option to deploy UI applications on top of it. But by using some trick we can do it. So let’s see how we can do this.

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 by following the steps below.

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 ubuntu:latest

RUN apt-get -y update

RUN apt-get -y INSTALL FIREFOX

Run apt get -y install xauth

EXPOSE 8887

CMD firefox

The above dockerfile contains the sequence of instructions to create an Ubuntu base image, runs an apt update on it, installs Xauth and Firefox. It then exposes port 8887 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

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

<username>/unix:  MIT-MAGIC-COOKIE-1  9288ef89ea3f431f92c1fe5ce8b6825

3. Build the Docker Image

Create the Docker Image using the following command.

docker build -t <image-name> .

Note- Make sure you are in the same directory where your Dockerfile is present.

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 Ubuntu 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

--

--