Running GUI applications on docker in linux

DockerOperating SystemOpen Source

Suppose you are building an application that requires user interface and pops up a window on running the script. And let’s say you want to run that script inside a docker container. Now, you might expect the docker container to run the UI application for you and display the same on your screen. But using normal docker run commands, you won't be able to see or interact with the UI application. You need to connect the display with the container in order to do so. In this article, we will discuss how to do exactly the same.

Here, we will see how to run a firefox instance inside a docker container and interact with it in your host machine. To do this, we need to first forward the X11 socket to the container so that the container can use it directly. We also need to forward the display environment variable. Even after doing this, it might fail because we have not yet set the permissions for the X server host. This might seem a hefty task at first. We will run you through all the steps in this article.

Let’s first have a look at the dockerfile which would help us to build the image.

# Set ubuntu as base image
FROM ubuntu

# Install dependencies
RUN apt-get -y update
RUN apt-get -y install xauth
RUN apt-get -y install firefox

#Expose a port number
EXPOSE 8887

# Run firefox
CMD /usr/bin/firefox

We have pulled ubuntu as a base image. After that, run the update on the newly formed ubuntu image. This will also form a new intermediate layer. Install xauth and firefox using the given commands. Xauth is a simple mechanism which would allow docker containers to access control of the X Servers also called the display servers. However, we need to manually add a randomly generated cookie for the session on which the X server is currently running.

To access the cookie, run the following command on your host machine.

xauth list

The output is of the form -

username/unix:0 MIT-MAGIC-COOKIE-1 8fe8efc75454dbf178bbe00442689406

The username will be your systems username. Copy the above output.

You can now try to build the docker image using the dockerfile created above using the following command.

sudo docker run -ti --net=host -e DISPLAY -v /tmp/.X11-unix <IMAGE NAME> bash

This would open the bash of the created ubuntu image.

Inside the bash, run the following commands.

  • xauth add <the output copied earlier>

  • Verify that the host system is connected to the remote system using this command− xauth list

  • Run the command - /usr/bin/firefox

This should open a firefox window which is running inside the container but is being displayed on your screen.

Note that, if an error such as {couldn't connect to display "−1"} persists, you need to replace −0 with −1 in the token which you copied earlier.

Performing similar steps, you would be able to run any GUI application inside the docker container and display and interact with them on your screen.

raja
Published on 01-Oct-2020 19:08:45
Advertisements