This example explains, how one can run GUI applications inside a docker container.
What is Docker?
It enables developers to package applications (in our case, a Machine Learning Predictor Script )into containers — standardized executable components that combine source code with all the operating system (OS) libraries and dependencies required to run the code in any environment. In simple terms, we would be able to run a Machine Learning Code in a separate OS with only the requirements needed for our script to execute.
GUI is a type of screen interface that is found on most computers, consisting of menus and icons that can be controlled by a mouse.
$ sudo docker run -it centos:latest
As you know, when you run the above shown cmd it will launch the docker container in your current system in which docker is installed.
If you try to run any GUI application on top of that container it won’t allow you to access the application as env DISPLAY is not specified.
So, in this example I had shown how to launch a firefox and Jupyter notebook application on the docker container. For running jupyter application we required a web browser so that’s the reason we are installing firefox to run jupyter on top of it.
Let’s Begin :
Run the xhost command for clients to connect from any host as shown below,
$ xhost +
The xhost command is a server access control program for X. It adds and deletes hostnames or user names to the list allowed to make connections to the X server(X server is used to provide GUI to container).
To check the environmental variable DISPLAY, use below command
$ echo $DISPLAY
then, export the env DISPLAY variable to :0 value(for creating environment variable)
$ export DISPLAY=:0
Run the Docker container using the below mentioned cmd,(as it specifies the environment and network option)
$ sudo docker run -it -- name task2 --env="DISPLAY" --net=host centos:7
now, install firefox inside the docker container
# yum install firefox
now, install python and jupyter (library)
# yum install python3 -y && pip3 install jupyter
Run firefox command as shown above to launch the firefox application on the Docker container.
Now, run cmd
jupyter notebook --allow-root
to launch the jupyter notebook using root.
As you can see the jupyter notebook, which is a GUI application is running on top of a Docker container!