DEV Community

Cover image for Run GUI Application inside Docker Container
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Run GUI Application inside Docker Container

Hello everyone,

In this blog we are going to launch GUI[Graphical User Interface] application inside docker container.

Before we start with main topic let's first discuss what containerization is and what is Docker?

Containerization

Alt Text

Containerization is a technology that created a virtual OS environment where we can work like we usually do in our system. It launches the new OS , Install the OS and boot the OS in seconds and it saves lots of time and solve many use case.

The tool or software that works on Containerization is Docker.

Alt Text

Prerequisite:

  • Linux OS
  • Docker installed

  • First start docker container

To start docker container use command systemctl start docker

  • Start container

To start container first we need to download image, to download image use command docker pull centos:7

now use command docker run -it centos:7 to launch docker container.
here centos:7 is image name with its version.

Alt Text

  • Install GUI application

here I am installing Firefox to check if it is running or not in docker container.

To install Firefox use command yum install firefox

Alt Text

Now launch Firefox using command firefox

Alt Text
here we can see it is asking for display as in containers no display environment variable are not present.

To solve this issue we need to launch new container with X Server.

What is X Server?

X is an application that manages one or more graphics displays and one or more input devices (keyboard, mouse, etc.) connected to the computer.

It works as a server and can run on the local computer or on another computer on the network. Services can communicate with the X server to display graphical interfaces and receive input from the user.For detail

Alt Text

Now launch the docker container using command
docker run -it --name dockergui --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" nitesh007/ml

Alt Text

Let's understand this command:

  • run :- this keyword will launch the container
  • -it :- this gives interactive terminal
  • --name :- use to set the name the name of container
  • dockergui :- it is name name of container, you can give any name
  • --net=host :- use to launch the container with host network
  • --env="Display" :- This is use to share the display of the host to the container
  • --volume="$HOME/.Xauthority:/root/.Xauthority:rw" :- This is use to share the host X server with the container by creating volume.
  • nitesh007/ml :- this is my docker image name that has all required packages used in machine learning.

After launching the container install the following software/packages.

  • Python3
    command: yum install python3

  • Scikit-learn
    command pip3 install scikit-learn( To run Machine learning model )

  • Jupyter Notebook
    command: pip3 install jupyter ( It is ide in which we will run ML model )

  • Firefox
    command: yum install firefox (To run Jupyter Notebook, browser is required )

Now we have installed required packages, it's time to run our model.
But for that we require dataset!

To copy dataset from base OS to container use command
`docker cp :

Now we have everything that is required.

  • Run Jupyter Notebook to run ML code

To launch Jupyter Notebook use command in the directory that has code jupyter notebook --allow-root

Alt Text
earlier it didn't launched as it is GUI application and container does not have display so it did not work earlier but now we can see it is working fine and we can work on it.

Alt Text

Alt Text

To watch the demo click on the video mentioned below⭐

Thank you!!

Top comments (4)

Collapse
 
agtoever profile image
agtoever

Although this works (and it’s very cool that it does work!), it poses a serious security risk, because it exposes your host’s Xserver to the docker container, and therefore, it compromises the sandbox feature of Docker. Recent research has shown that a serious percentage of Docker templates contain (serious) unpatched security risks.

An alternative route would be to secure your host’s Xserver with AppArmour or SELinux. Or, run a vnc server within you Docker container and connect using vnc.

Collapse
 
niteshthapliyal profile image
Nitesh Thapliyal

Thanks for telling I didn't knew that , I will do more research on this.😄

Collapse
 
dc31xx profile image
dc31xx

If you are into this, you may find this interesting:

Collapse
 
niteshthapliyal profile image
Nitesh Thapliyal

That's awesome, sure will look into it, thanks for sharing it.