Suppose you are building a UI application and deploying it on a docker container. If you want that UI application to display the user interface screen on your local machine while running the application inside the docker container, you will have to connect the display of the Docker container with the display of Local Machine. So, here we want Jupyter Notebook which runs on a browser to be run inside docker container.
Let suppose, you want to run firefox browser inside a Docker container. So first, lets have a look at type of applications and how it runs. There are majorly two types of applications that can be containerized.
- Applications that run as a Background service eg: webserver
- GUI Application that run in foreground.
So, for any GUI application to run, we need to have XServer which is already available in all Linux systems.
Lets understand what is XServer?
The X server is a single binary executable(/usr/x11R6/bin/Xorg)[in rhel8] that dynamically loads any necessary X server modules at runtime from the /usr/X11R6/lib/modules/ directory. Some of these modules are automatically loaded by the server, while other are optional. The X server and its configuration files are stored in /etc/X11/ directory. The configuration file of X server is /etc/X11/xorg.conf.
But, when we launch a container, we don’t have Xserver configured here, so what we can do is,
- we can share the Host’s XServer with the container by creating a volume.
--volume = “$HOME/.Xauthority:/root/.Xauthority:rw”
we have to share the host display environment to the container.
Also we have to run container by providing these options.
Now let’s begin with the practical,
Pre-requisite for this practical: yum configured, docker installed and enabled on your system.
Step 1: Checking availability of docker\
First thing you required is docker installed in your system. We can check it by using this command,
Step 2 : Pull centos image
First you have to install docker image. You can choose any image for it. Here i am going to install centos latest image by this command,
docker pull centos:latest
In my case centos image was already installed that’s why output is like this. IF this image is not installed in your system, then download it from this command and it will take some time to install according to your network speed.
Step 3: Launch Container
Main step to launch gui application on docker container is here. With run command we have to pass some paramenters which are discussed above. Command is :
docker run -it --name lwtask2 --net=host --env=”DISPLAY” --volume=”$HOME/.Xauthority:/root/.Xauthority:rw” centos:latest
In my case , name of container is lwtask2, you can give any name according to your choice.
Step 4: Install required packages inside docker container
After launching the container , now we have to install some packages/softwares in it.
First we install python3 with the following command,
yum install python3 -y
Secondly we install firefox using command given below,
yum install firefox -y
Next step is to install jupyter notebook. We can install this with pip3 command,
pip3 install jupyter
now we are done with all installations, let’s check it.
Step 5: Trying to launch GUI applications on Docker
Now trying to open firefox using command line and check whether is is working or not.
To open firefox , just write firefox only.
AMAZING, We have done with our main task that is running GUI appliaction on top of docker container. As you can see in above image, after running firefox command, browser is launched inside docker container.
Now let’s check the same for jupyter notebook, but it is not preferred to launch jupyter notebook with root account directly. So here we have to use --allow-root option with our command,
so command to launch jupyter notebook will be like:
jupyter notebook --allow-root
You can see jupyter notebook is launched successfully on docker container.
Similarly we can run other applications on docker container .
Thanks for reading………..