Running GUI Applications in a docker container.
Task Description:
1) Launch a container on docker in GUI mode
2) Run any GUI software on the container
In this article , we will be launching firefox(GUI) in a docker container.
Docker is normally used to containerise background applications and CLI programs. However , you can also use docker to run graphical programs(GUI).
Containerised graphical apps are useful when you’re evaluating software or need to run two versions of a package. You can use programs on your existing desktop without needing to touch your host’s configuration.
Docker has a of advantages and one of them is to be able to use apps with a GUI isolated in a docker container. For example your Browser(firefox), TextEditor , jupyter notebook, and etc.
A Docker container is a form of encapsulation which seems to be superficially similar to a virtual machine. Unlike a virtual machine, containers share the same Linux kernel as their host system.
Why Run GUI Apps in Docker?
Running a GUI program in Docker can be a useful technique when you’re evaluating a new piece of software. You can install the software in a clean container, instead of having to pollute your host with new packages.
This approach also helps you avoid any incompatibilities with other packages in your environment. If you need to temporarily run two versions of a program, you can use Docker to avoid having to remove and reinstall the software on your host.
To perform this task , we first need to have docker installed in our system.
We can perform this task by following the steps given below :
Step 1: First open docker using the command:
systemctl start docker
Step 2: Then , use the command:
docker pull centos:7
Step 3:Then , we will run a container through this image , use the command:
docker run -it –name=<image_name> <image_tag>
Here, it will be : docker run -it –name=GUI_OS centos:7
Step 4:After this, we need to install firefox for which we will be using the command:
yum install firefox -y
Step 5: Then ,we will make a separate directory for storing this task using the command:
mkdir directory_name
Here, it will be: mkdir task2
Step 6: Then, go in to the directory using the command:
cd directory_name
Here it will be: cd task2
Step 7: Then use the command :
vim dockerfile
Step 8: And inside this write the following:
FROM centos:7
RUN yum install firefox -y
CMD [“/usr/bin/firefox”]
The RUN command in here lets you execute commands inside of your Docker image. And the CMD command allows you to define a default command to run when your container starts.
Step 9: Once done, go out of the vim file by using wq and we have to build our custom image by using the command:
docker build -t firefox .
Now, we have successfully built our firefox in our docker container
Step 10: Now we have to run this container by passing an environment variable as a DISPLAY and a network as a host by using the following command:
docker run -it --name <os_name> --env=DISPLAY --net=host <image_name>:<tag>
Here, it will be : docker run -it — name=GUI_OS — net=host — env=’DISPLAY’ firefox
The display variable above is used to indicate to graphical applications where it will have to display the actual graphical user interface and the Docker option — net=host configures the Docker container to share its network stack with the host
With this ,we have successfully launched the firefox(GUI) on the docker container.