How to run GUI Applications in Docker Container

How to run GUI Applications in Docker Container

Today's task is to launch a GUI (Graphical User Interface) application on top of a Docker Container, so first of all,

What is Docker?

f55e8059ea945abfd6804b887dd4a0af.gif

Docker is an open-source containerization platform. Docker enables developers to package applications into containers—standardized executable components that combine application source code with all the operating system (OS) libraries and dependencies required to run the code in any environment.

While developers can create containers without Docker, Docker makes it easier, simpler, and safer to build, deploy, and manage containers. It’s essentially a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation.

Why use containers?

Swarmnado-357x627-30-1.gif

Containers are made possible by operating system (OS) process isolation and virtualization, which enable multiple application components to share the resources of a single instance of an OS kernel in much the same way that machine virtualization enables multiple virtual machines (VMs) to share the resources of a single hardware server.

Containers offer all the benefits of VMs, including application isolation, cost-effective scalability, and disposability. But the additional layer of abstraction (at the OS level) offers important additional advantages:

  • Lighter weight: Unlike VMs, containers don’t carry the payload of an entire OS instance—they include only the OS processes and dependencies necessary to execute the code.

  • Greater resource efficiency: With containers, you can run several times as many copies of an application on the same hardware as you can using VMs. This can reduce your cloud spending.

  • Improved developer productivity: Compared to VMs, containers are faster and easier to deploy, provision, and restart. This makes them ideal for use in continuous integration and continuous delivery (CI/CD) pipelines and a better fit for development teams adopting Agile and DevOps practices.

In Docker containers, we mostly work in CLI but this time we create a Docker Image in which we will use GUI application, so let's start:-

Step 1: Setup Docker Yum Repo and download Docker Software.

Go to the /etc/yum.repos.d/ and setup docker.repo :- docker repo.png reposd.png

[docker]
baseurl = https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck = 0
name = docker repository

Docker Yum repo is configured, now download the docker software by simply using yum install docker-ce --nobest -y.

It will take some time to download and install the docker, if downloading fails try again after some time. Now the docker is installed in our system, we can confirm by using the command rpm -q docker.

dockerce.png

Step 2: Download Docker Image form Docker-hub.

Docker container uses Docker images, we can download docker image either by visiting hub.docker.com or we can download by using docker pull <image_name>:<version>.

If we don't specify the version it will by default pull the latest version of that image, here I'm downloading the docker centos image by using docker pull centos.

pulling image.png

Step 3: Create a Dockerfile.

Now our centos image is downloaded, let's create a Dockerfile (A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.)

creating docker file.png

insidedocker file.png

FROM centos
RUN yum install firefox -y
CMD ["/usr/bin/firefox"]

Step 4: Build the Dockerfile.

Build the image from the Dockerfile using docker build -t <name_you_want_to_give> <path>.

buildingimage.png You can check the images by using docker images command.

dockerimages.png

Step 5: Create a Docker Container using GUI image.

Now at last create a container using the container by running command docker run -it --env="DISPLAY" --net=host <image_name>, be sure to set the environment as DISPLAY as it is a GUI application. image.png After few seconds the firefox will open up which will be running from the docker container:- firefoxx.png So, now our task is completed of running GUI application in Docker Container.

In the end, thanks to Mr Vimal Daga sir for teaching this great content.

Thank You For Reading :-)