Running GUI Applications in Docker Container

I’m using tens of different tools and frameworks every day. And mostly, some of the tools I used for the specific project. So, after completing that project, I will rarely use those tools in everyday work. The installation of tons of tools on a regular machine makes them reside there for a longer time and I have to regularly invest my time in removing these tools. One of the great ways is to use Docker containers for using these tools. But when I’m working with frameworks like Django or Flask it is hectic to use vi or vim editor in the docker container. So, we have to use IDEs to work with these frameworks. But the main problem is that most of these tools are GUI tools and when you try to run them in a docker container you may get an error that says display not found.

In this article, I will show you how to get over this problem and run GUI applications in docker containers.

So, let’s begin…

Pre-requisite:

Docker should be installed.

Ways for Running GUI Applications with Docker:

There are some ways to run GUI applications in Docker containers like using the X server or using VNC. I’m will be using the X server in this article.

What is X Server:

X server is a windowing system for bitmap displays, common on Linux operating systems.

The X11 server (usually Xorg these days) communicates with clients like xterm, firefox, etc via some kind of reliable stream of bytes. A Unix domain socket is probably a bit more secure than a TCP socket open to the world, and probably a bit faster, as the kernel does it all, and does not have to rely on an ethernet or wireless card.

Creating a Dockerfile:

First of all, we will create a Dockerfile to create a Docker image with firefox installed.

Dockerfile

This will create an Ubuntu image with firefox installed. Also, this image will have a user ankush without a password.

Build a Dockerfile:

$ docker build -t firefox .
docker build -t firefox .

This command will create an image firefox.

Now, we are all set to open a firefox web-browser in the docker container.

Open Firefox in Docker container:

Run the following command to open firefox in the docker container.

$ docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox

Before executing this command, let’s understand what this command will exactly do.

docker run -ti — rm

-i sets up an interactive session; -t allocates a pseudo tty; --rm makes this container ephemeral.

-e DISPLAY=$DISPLAY

-e sets the host display to the local machine's display.

-v /tmp/.X11-unix:/tmp/.X11-unix

-v bind mounts the X11 socket residing in /tmp/.X11-unix on your local machine into /tmp/.X11-unix in the container.

firefox

firefox is the name of the image that we had built earlier using Dockerfile.

Finally, run the command, and hopefully, you will see your Firefox Web-Browser is running inside the Docker container.

Optimizing the above command:

We can replace -e DISPLAY=$DISPLAY with -e DISPLAY. Docker will take care of export for us.

So, the final command will be

$ docker run -ti --rm -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox

Run this command and you will get the same result.

Running Firefox in Docker Container:

By using this concept, you can run your complex environments like Django or Flask or Java, or any other projects in the Docker containers with their IDEs.

For any help or suggestions connect with me on Twitter at @cankush625 or find me on LinkedIn.

Tech blogger, researcher and integrator