Running GUI Based Applications Inside A Docker Container

Mayank_Agarwal
3 min readApr 30, 2021

Today, the entire IT world is utilizing the power of a container to do some amazing stuff. No doubt, containerization has turned out to be a boon for the tech market. (Here, I am particularly talking about the Docker)

  • Most of the times you have heard that people generally use containers for Enterprise Server Applications. But standing alone from the crowd, is it possible to use containers for a Desktop environment?

And the answer is a big YES.

  • Did you know, that the Docker container has a huge scope when it comes to impacting the Development Environment?
    The best part about Docker is that — using docker, you can containerize almost every single technology you work on. From launching the environment to deploying some real servers on it — just a second away!
  • Now, the most important question — How is it possible to run a GUI based app inside Docker? Here is the answer:

If we talk about the running nature of any application, you can find only two of them:

  • Applications that run in the background such as webservers.
  • Applications that run in the foreground (generally, GUI based) such as a web browser.

In this blog, I am going to show you how to run this second type of application inside a docker container.

Let’s first try to run a GUI application (say, Firefox) inside a docker container.

To launch a docker container with ‘centos’ as its base image:

docker run -it --name gui centos

After launching and getting inside the container, let’s install firefox:

yum install firefox -y
  • Yum is the package manager of Centos. And you do not need to configure inside the docker because the centos image comes with pre-configured yum.

After installing Firefox, if you try to launch firefox using ‘/usr/bin/firefox’ command, it will show the above error:

The error says that a “DISPLAY” environment was not found. So, here are the steps we are going to perform in order to solve the error:

  • Create a Docker image that has firefox installed
  • Launching container by providing it with a DISPLAY variable. Also, we will launch it in a ‘host’ network. Host network by default exposes a container so that it can be directly used from the base OS.

Let’s try to carry out the same:

Dockerfile for creating the docker image:

FROM centos
RUN yum install firefox -y
CMD ["/usr/bin/firefox"]

Creating the docker image:

docker build -t docker_gui .

I have created the same:

And now, it is the time to launch the container:

docker run -it --env="DISPLAY" --net=host --name firefox docker_gui

And the GUI application (firefox) has been launched inside the container:

That was all as of now. I hope you liked the above blog.

  • This practical was just a small demonstration of the magical stuff that can be done using Docker.
    Docker has an amazing scope in the IT world. Top MNCs are leveraging the benefits of Docker to a huge extent.

--

--