Who thought that a boring and monochrome life of a docker container could be made colorful? Who thought that the boring ad-hoc could give graphical interface?

Well, finally the day has come when docker will witness the dash of GUI and experience the joy of being polychromatic. In this blog, I have demonstrated ways to launch an GUI application (Jupyter Notebook) on docker container. So lets go…!

Task Description :-

🔅 Run any GUI software on the container

Summary :-

Python is an high level, interpreted, interactive, object-oriented programming language. It is a general-purpose coding language which means that, unlike HTML, CSS, and JavaScript, it can be used for other types of programming and software development besides web development. That includes back end development, software development, data science and writing system scripts among other things.

Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning.

Graphical User Interface GUI is a system of interactive visual components for computer software. A GUI displays objects that convey information, and represent actions that can be taken by the user. The objects change color, size, or visibility when the user interacts with them.

X Window System (also known as X11, or simply X) is a client/server windowing system for bitmap displays. It is implemented on most UNIX-like operating systems. The X architecture provides a fairly primitive client/server framework for a graphical display, including drawing and moving windows on the display and interacting with input devices such as keyboards, mice, tablets or touchscreens.

The three ways to connect to an X server are —

  1. Use ssh with X forwarding
  2. Use Xauthority and xauth
  3. Use host or user control with xhost

Here, in this blog, I have implemented the task using Xauthority and xauth.

Solution :-

Setup yum and docker repository. And then install, enable docker services.

Commands used —

systemctl restart docker

systemctl status docker

🔲 Step 2 —

Lauch a new container along with few more arguments that will help us enable the display unit of the docker.

Note —usually when we run a container, in the ad-hoc section, we see the id of the container being displayed. But after running this command, instead of the container id , ”root@localhost” will be displayed.

This is a complex command which can be broken down as follows —

  • run : a docker module that starts the container
  • it : used to specify that user wants interactive session (i) with a tty (t) attached which helps in issuing commands and reading the output. The -t (or — tty) flag tells docker to allocate a virtual terminal session within the container. It’s is commonly used with the -i (or — interactive) option, which keeps STDIN open even if running in detached mode.
  • name : Assign a name to the container (here the name of the container is given as “guicontainer”)
  • — net=host : this option is used to make the programs inside the docker container look like they are running on the host itself, from the perspective of the network. It allows the container greater network access than it can normally get.
  • — env=”DISPLAY” : it is a display variable which can also be used to adjust the screen, if default screen size doesn't fix the window, using DISPLAY_WIDTH and DISPLAY_HEIGHT variables.
  • — volume=”$HOME/.Xauthority:/root/Xauthority:rw” : here we are sharing the Host’s XServer with the Container by creating a volume.
  • centos : name of the image used which is used to create the architecture of the container.

Commands used —

docker run -it<space> — name guicontainer<space> — net=host<space> — env=”DISPLAY”<space> volume=”$HOME/.Xauthority:/root/.Xauthority:rw” centos

docker run -it --name guicontainer --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" centos

🔲 Step 3 —

Install python and then using pip download all the required modules/libraries. Here I'm downloading pandas, matplotlib, numpy, scikit-learn, jupyter.

Commands used —

yum install python3

pip3 install pandas

pip3 install matplotlib

pip3 install numpy

pip3 install scikit-learn

pip3 install jupyter

🔲 Step 4 —

After all the required modules are downloaded, run the following command to access the jupyter notebook using CLI (command line interface). The following command will then help us launch the jupyter notebook by copy/pasting the links mentioned in the terminal.

The links mentioned below can be used to launch the GUI Jupyter Notebook Application on Web Browser
Pasting the 2nd link from the terminal on to firefox

Commands used —

jupyter notebook<space> — allow-root

jupyter notebook --allow-root

So hereby, I conclude my task number 2 which involved many other concepts and demonstration of GUI application on Docker Container.

Thank You..!

Full Stack Web Developer | Ansible | Docker | Kubernetes | OpenShift | RedHat | AWS | Azure | GCP