GUI applications over Container!

Rishi Agrawal
4 min readMay 28, 2021

Wait! did you just read this title, GUI and containers how? Containers are so lightweight OS that they only have CLI and all people in the world considered to use as a CLI tool. Why you want to launch GUI applications over the containers?

So, yes we can launch GUI applications over the containers, we just need to configure the container in the right way. But what is the use-case? So there are many use-cases, If you are creating a GUI based application you might want to test it, or you want use IDE for writing some code, and many more.

Let’s create a scenario to understand this: Consider Bob, he is ML engineer by profession and wanted to make a container that can run Jupyter notebooks. To run the Jupyter notebook we need web browser first. But Bob is new to this containers world so he don’t know that containers don’t support GUI.

Before installing Jupyter he decided as he is new so lets test with first installing Firefox. He immediately jumps to his RHEL8 machine which has docker installed. And runs the following command:

# docker run -it --name guitest centos 

After this first installs the Firefox.(inside the container)

# yum install firefox
launching container and installing Firefox

After installation completes, he rum the command firefox to start, instead of starting it gives the following error.

Error message

After reading error, Error: no DISPLAY environment variable specified. Bob came to conclusion we have to add environmental variable named DISPLAY.

How to add? and checks the docker documents and found that in docker run we can provide environmental variable using option -env=Display

screenshot of Docker documentation.

He exits the current container, and starts a new one with option env

# docker run -it --name guitest1 --env=DISPLAY centos

And he reinstalls Firefox and run the command to run firefox but still he got the error.

After seeing this error, he concluded that there is network issue, here what happens is docker to show GUI app connects to host via network and currently the network is bridge. We need to make it host in order to make the container can connect to host OS to show GUI apps.

Ones again, Bob goes to document and search is there any option that can change network from bridge to host only.

He found there is a option called --net to change the network in docker run command after exiting previous .

# docker run -it --name guitest3 --env="DISPLAY" --net=host centos

and installs the firefox to test this time. He runs the firefox command this time and a window pops up i.e. that's a GUI application running from the container guitest3!!!(ignore all the warnings)

Now, Bob knows how to run GUI apps in the containers. So, he installs python3 and the required libraries for his ML.

# yum install python3
# pip3 install jupyter scikit-learn joblib pandas numpy

Run the following command to start jupyter notebook running inside the container.

# jupyter notebook --allow-root

yay! Bob did it!!

Conclusion:

  1. By using this method, you can use any GUI based application, in a docker container.
  2. This blog also shows how to solve errors if you come across, I have used a way which is effective and efficient.

That’s it for this Blog! Huge thanks to Mr Vimal Daga sir for teach right concepts.

--

--

Rishi Agrawal

Aspiring MLOPS engineer with Multi-cloud and Flutter/MERN