Running a GUI on Docker Container

Sourabhmiraje
5 min readMar 16, 2021

Hey folks,

We all know how the docker is ruling the devops world with their ability to launch a lightweight containers. We use the docker containers to run a single application almost all the times but they are CLI based many of time.

but, What if we want to launch the container which supports the GUI. is it possible ? if yes, how ?

I know you guys might have a lots of Question after reading the title of this article. but don’t worry at the end of this article you will get all of your answers.

In this article we are going to see some practical how to launch a GUI app on docker. before that we will se some theoretical concepts required for todays practical.

What is Docker?

Docker is an open source project that makes it easy to create containers and container-based apps. Originally built for Linux, Docker now runs on Windows and MacOS as well.

Dockerfile

To run every container in docker we need a docker image. we can create docker image in two way, viz. docker commit and Dockerfile.

Dockerfile is used mostly. each Docker container starts with a Dockerfile. A Dockerfile is a text file written in an easy-to-understand syntax that includes the instructions to build a Docker image (more on that in a moment). A Dockerfile specifies the operating system that will underlie the container, along with the languages, environmental variables, file locations, network ports, and other components it needs and, of course, what the container will actually be doing once we run it.

Display Server

Source: Wikipedia

A display server or window server is a program whose primary task is to coordinate the input and output of its clients to and from the rest of the operating system, the hardware, and each other. The display server communicates with its clients over the display server protocol, a communications protocol, which can be network-transparent or simply network-capable.

X11 : Display server communications protocols

One example of a display server is the X.Org Server, which runs on top of the kernel (usually a Unix-based kernel, such as Linux or BSD). It receives user input data (e.g. from evdev on Linux) and passes it to one of its clients. The display server also receives data from its clients; it processes the data, it does the compositing and on Linux it passes the data to one of three kernel components DRM, gem or KMS driver. The component writes the data into the framebuffer and content of the framebuffer is transmitted to the connected screen and displayed. X relies on GLX.

X Window System

The X Window System (X11, or simply X) is a windowing system for bitmap displays, common on Unix-like operating systems.

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.

X uses a client–server model: an X server communicates with various client programs. The server accepts requests for graphical output (windows) and sends back user input (from keyboard, mouse, or touchscreen). The server may function as:

  • an application displaying to a window of another display system
  • a system program controlling the video output of a PC
  • a dedicated piece of hardware

X provides display and I/O services to applications, so it is a server; applications use these services, thus they are clients.

To use an X client application on a remote machine, the user may do the following:

  • on the local machine, open a terminal window
  • use ssh with the X forwarding argument to connect to the remote machine
  • request local display/input service (e.g., export DISPLAY=[user’s machine]:0 if not using SSH with X forwarding enabled)

The remote X client application will then make a connection to the user’s local X server, providing display and input to the user.

I hope now you got a clear idea about some new terminologies.

Now lets jump to the practical part…

Lets create the docker file

Step 1:

Create a folder with anyname of your choice, go inside the folder and create a new file with name Dockerfile compulsory and add the following code to it.

Note: here I’m using ubuntu:latest, you can choose your own base image. commands will change accordingly.

FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install xauth
RUN apt-get -y install firefox

Step 2:

build the above image with the following command.

docker build -t your_image_name:version .

(. is used beacuse I am running the above command in the same directory where I created dockerfile)

After build we need to export the DISPLAY env variable to 0.0

export DISPLAY=:0.0

This sets the environment variable $DISPLAY to be :0.0. When you run an X-windows program, this tells the program where to find the X server, and which display on the X server to use.

When we try to display something in X that is run by a different user than the owner of the current display (the logged in user) we need to set this variable 0.0 .

Step 3:

Now run launch the new container with the new image created.

sudo docker run -ti --net=host -e DISPLAY <IMAGE NAME> bash

we use network adapter of host so that we have all the connectivity same as base OS and DISPLAY environment variable as we set it to 0 so that any user other than root can use X server.

After launching the container, launch the firefox with command

/usr/bin/firefox in bash terminal.

Output will be as shown below…

If you get the above screen then you are done successfully with this practical.

Conclusion:

Though container are lightweight OS system, it is possible to launch a GUI application on the container with the help of X server. we can use this approach for various usecases like ML model testing and many things.

I hope now your all questions are cleared. I hope this was helpful to you.

Please let me know in the comment.

You can contact me on my LinkedIn profile below.

--

--