Running a GUI Program in Docker Container

How to run a GUI Program using Docker Container?

MishanRG
Nerd For Tech
Published in
5 min readJun 1, 2021

--

Greetings Geeks!!! I hope you are here to find something interesting.

So let’s get started with the blog!!!

Introduction

We all might know about docker. Docker is easy to start, fast to boot, and really comfortable. If you are new to this technology, you might want to check out my blog on what Docker is and how we can set up Docker in our system. You can check out my blog on how to set up Docker in RedHat Linux 8.

Docker is swift, and one of the reasons is that it is light. It can launch container/OS in just some seconds. If we go deeper, it is actually a process run by some other program that makes it faster(we have more in there). And Docker has all of its program which is compatible with CLI. Mostly all program in Docker is CLI program which doesn’t need any graphics card or graphics to use.

So the question comes how to run any graphical program using a Docker Container? Yes, we will not be actually running the GUI program inside the container because Docker doesn’t have any GUI support. Still, we will install a GUI software inside Docker, use the Docker container resources, run the program, and see the graphics.

Practical

Let’s start with the practice now. We have docker software available in our system. I am using RedHat ad my base system where docker will be launched.

First, let me show my Docker version:

As we can see, I am using Docker 20.10.6. And this also means that Docker is successfully installed in my system.

The command “docker ps -a” shows if any container is running or not. In our case, we don’t have any. FRESH START.

Image Creation

Docker containers are created with images. Usually, the image doesn’t come with a GUI program in that pre-build image, so we have to create our own image with a GUI program inside. We are using Dockerfile to create an image. We have created a directory, and inside it, we created a Dockerfile, so later, when we try building the image, it will auto-detect this file and use it.

So inside our Dockerfile, we have written some code. We have used the centos image to build our custom image, and there we have installed firefox using RUN, and then we have used CMD to run the firefox program. So when this image is built, it will have firefox installed, and when someone runs/launch a container with this image, it will directly run firefox, and we know firefox is a GUI program.

Now before we build our image, image let’s see the available image in our system. We use the command “docker images.”

We can see the images available in the above images. Now we will build our image with the Dockerfile as we want. For this, we use the command:

$ docker build -t firefoximage:v1 .

Our image creation process begins, and when we check the available images, we can see in the below picture that one new image comes up, and its name is “firefoximage,” as we have used in the above image.

Now, as we have the image, let's try creating a container with that image. We can use the command:

docker run -it --name=<name for container> <imagename>

In the above image, the docker container failed, and it said that no display was available to run our firefox program.

Let me explain this, we need a GUI screen to run the GUI program, and the container doesn’t have any GUI screen or any driver or software that runs GUI, so we use our host system GUI and ask Docker to use that GUI to run its program. So to do that, we need to give Docker an env variable DISPLAY. It holds the display socket name which we would like to use. The DISPLAY refers to the primary monitor if used PC. Then we have used--net= host some networking concepts internally, and in short, we are using the Host RedHat System network card for the Docker.

In the above image, we can see that the container is running in the background, and then in the front ground, the Firefox browser has started. This means we have successfully launched a GUI program in Docker. We can use Ctrl+C to stop the program.

Conclusion

We have successfully launched a Docker Container, which can host a GUI application inside it. We have used some twists and tricks and completed the task.

I hope I have explained everything, and if you have any doubts or suggestions, you can comment on this blog or contact me on my LinkedIn.

Thank you for staying until the end of the blog, and please do suggest some ideas for improvement. Your suggestions will really motivate me.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

MishanRG
MishanRG

Written by MishanRG

I blog about ML, Big Data, Cloud Computing. And improving to be the best.