Task Description -
Run a Docker container that can run GUI (Graphical User Interface) Programs
Launch a container on docker in GUI mode.
Run any GUI software on the container
Prerequisite
Docker Installed on your system.
Docker Host should be GUI-based.
Note: I’m Using RedHat Enterprise Linux V8 in this practical.
What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production. (source: docs.docker.com)
Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.
Developing with Docker
Developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage create enormous complexity. Docker simplifies and accelerates your workflow while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.
Problem
Sometimes we need to run GUI programs in containers to debug or test the things but most of the people stuck there that How to run GUI programs in a docker container.
So, Lets get Started
It is very easy to run GUI programs in a docker container.
So, first, we have to Launch a Container from some base image, here I’m using the official centos
Linux docker image. And while launching container give environment
variable $DISPLAY
to container and in Volume
of container mount the path of x11
server/display. Give a name
to your container and launch it.
docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ --name GUIcontainer centos
That’s all we need to do, Now we can Run any GUI program in this container.
So, in this practical, I’m Gonna Run three GUI software —
- Firefox (Web browser)
- Jupyter notebook (python IDE)
- Gedit (Text editor)
Run Firefox Web Browser
Firefox is a Graphical user interface program. For this program, we need a graphical display to Run.
So, first, we have to install Firefox
browser in our launched container.
yum install firefox -y
Now, Run firefox
command to launch the browser.
firefox
Run Jupyter Notebook
For launching Jupyter notebook
we need python3
interpreter/package to be installed in container. so, first install python package by running the below command.
yum install python3 -y
Now, through pip3
command install jupyterlab
for using jupyter notebook
pip3 install jupyterlab
Now after installing jupyterlab
Run jupyter notebook
Here see carefully, we have to launch jupyter notebook
with root privilege
by adding --allow-root
option in jupyter notebook
command.
jupyter notebook --allow-root
Here, we can see jupyter notebook
is launched successfully. we can write our python code here and Run it very easily.
Jupyter notebook
is a web app, this is why it launched in Firefox
browser, that’s why we need to install Firefox browser before running jupyter notebook
Jupyter notebook launched successfully.
Launch Gedit (Text Editor)
Now install gedit
package in the container. It is a GUI-based Text editor.
yum install gedit -y
After Installing gedit, run it, and write anything whatever you want to write.
gedit
text editor is very easy to use.
Note : gedit <text_file_name>
gedit docker
Write anything you want to write in this file and save it.
Here I have written a very simple python code.
If you have written some code then you can execute/run it.
Or if you have written something else then you can see that by using cat
command or by again opening gedit
My python code is executed successfully.
That’s All, Keep Learning.
! THANK YOU For Reading !