Run GUI Application in Docker Container

Riyadaga
3 min readMay 31, 2021

--

In this blog, I’m going to show how to launch a GUI application in the Docker container. We can run any GUI application inside docker. Here I’ve launched Firefox web browser in container base image- CentOS with RHEL8 as a base Operating System to demonstrate.

First, let's get familiar with basic terminology.

What is Docker?

In simple terms, Docker is a software platform that simplifies the process of building, running, managing and distributing applications. It does this by virtualizing the operating system of the computer on which it is installed and running. It provides the ability to package and run an application in a loosely isolated environment called a container.

Docker enables a faster software delivery cycle that helps in easy scaling to meet demand and easy updating to add new features according to business requirements. We can use Docker to set up all the dependencies (components, libraries and system configurations) for a program we want to run, without having to worry about other programs having conflicting dependencies.

What is GUI?

GUI is a Graphical Interface that is a visual representation of communication presented to the user for easy interaction with the machine. GUI stands for Graphical User Interface. It is the common user Interface that includes Graphical representation like buttons and icons, and communication can be performed by interacting with these icons rather than the usual text-based or command-based communication.

GUI of VirtualBox

Now, let’s begin!

Docker normally used to containerize background applications and CLI programs. We can also run graphical programs though. Here we will use the existing X Server, where the host machine is already running a graphical environment. A better approach is to mount your host’s X Server socket into the Docker container. This allows our container to use the X Server we already have. GUI applications running in the container would then appear on our existing desktop.

Installing Docker

yum install docker -ce --nobest

  • Install docker using the above command.

systemctl start docker

systemctl enable docker

  • Start and enable the docker services before preceding ahead.

docker pull centos:latest

  • Pull centos image from Docker Hub.

Create a Dockerfile

We’ve downloaded the centos image. now we’ll create a Dockerfile(it is a simple text file that consists of instructions to build Docker images). I’ve used vim to edit the file.

Build the Docker Image

“docker build -tag <imagename:version> path”

Launch the Container

--net=host

  • To run the container with the host network driver.

--env=DISPLAY

  • To share the DISPLAY environmental variable to the container.

-v /root/.Xauthority:/root/.Xauthority

  • Share hosts X server by creating container volume.

The command will look like this-

docker run -e DISPLAY -v /home/root/.Xauthority:/root/.Xauthority - -net=host <image_name>

Result

We have successfully launched the GUI application- Firefox in our container.

firefox is running

Thanks for reading! :)

--

--