Run GUI Application inside Docker Container
Tags: docker, machinelearning, jupyter
Published: 2021-05-30 18:16:31

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pggg6nnwqknlpzkox321.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddxfghvbpngjv7skhlen.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yfq79og5ozy3niysv1d0.png)


- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vk3kq1t5p8yiu1vq7be2.png)


Now launch Firefox using command `firefox`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7v71h8h7an5kn07g4ow.png)
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](https://askubuntu.com/questions/7881/what-is-the-x-server)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzt7aidph5aykmo4mnrh.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcjizhqufcvfs8ui9jk6.png)

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 <path-of-file> <container-id>:<destination-path>

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j1lal2rp4lgkgdjsxohq.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k66g9yo4v9nroksoshmp.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrrw8xue0stn1u7zxpmf.png)

To watch the demo click on the video mentioned below⭐  

{% youtube LR0sOnxSO5Y%}

Thank you!!