Hello everyone,
This blog is about how we can run a GUI [Graphical user interface] application inside a docker container.
Before we start with the main topic let’s first discuss what is docker and containerization?
Containerization
Containerization is a technology that created a virtual OS environment which is normally like any system.
It launches the new OS, Install the OS and boot the OS in seconds and it saves lots of time and solves many use cases.
The tool or software that works on Containerization is Docker.
Prerequisite:
- Linux OS
- Docker installed
- First, start a docker container
To start the docker container use the command Systemctl start docker.
- Start container
To start the container first pull an image, to pull image use command docker pull centos:
now use the command docker run -it centos
to launch the docker container.
here centos:7 is the image name with its version.
- Install GUI application
I am installing Firefox browser to check whether it is running or not inside docker container.
To install Firefox use the command Yum install Firefox.
Now launching theFirefox using the command Firefox.
here we can see it is asking for display as in containers no display environment variables Specified.
To solve this issue we need to launch a 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.
Docker by default does not support GUI applications to run on the containers. Firefox is a graphical program and it needs environment variables to run itself. that’s why we will tell the container to use the Host OS (in my case RedHat 8) GUI screen and for that, we will use the environment variables using the option “env” in the docker run command.
Now launch the docker container using the command**docker run -it --name dockergui --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" nitesh007/ml**
Let’s understand this command:
- run:- this keyword will launch the container
- -it :- this gives an interactive terminal
- — name:- use to set the name of the container
- dockergui :- it is the name of the container, you can give any name
- — net=host :- use to launch the container with the host network
- — env=”Display” :- This is used to share the display of the host to the container
- — volume=”$HOME/.Xauthority:/root/.Xauthority:rw” :- This is used to share the host X server with the container by creating volume.
After launching the container install the following software/packages.
Now let’s install firefox inside it and let’s see whether it works or not.
Installing Firefox
Launched Firefox inside Container
earlier it didn’t launch as it is a GUI application and the container does not have a display so it did not work earlier but now we can see it is working fine and we can work on it.
Thankyou !!!