Run GUI Applications in Docker container

Siva Naik
4 min readNov 6, 2022

Hey Folks! Hope you are doing amazing…

In this blog, I am going to share how you can launch the application with Graphical User Interface in Docker.

Docker

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first started in 2013 and is developed by Docker, Inc.

GUI

GUI is Graphical User Interface, It’s an interface for the user to interact with Operating System.
There are different types of interfaces to interact with OS

  • GUI Graphical User Interface
  • Command Line
  • API — Application Programming interface

The mentioned above 3 are different components in Linux, If we want to test we can completely remove the Graphics component and wework only with Terminals.

Xserver — Graphics component in Linux

Xserver is that which provides the Graphical User Interface in RedHat. It’s also called the Graphics Manager, We can change Graphics Manager to our preferences.

An X server most likely refers to the X11 windowing system, which is the GUI that most Unix flavors (including Linux) use.

  • Xserver is the client-server architecture

Xserver is the GUI protocol to display the GUI of applications, Xserver can be the client for applications running locally as a single machine, or I act as a client for instances running remotely.

  • We can change some environment variables to connect with the GUI running in the Docker container
  • We are going to launch the container with the host network interface

Prerequisites:

  • Docker

You can install Docker here,

Installing Docker in RedHat 8
By Default from RHEL 8, RedHat officially doesn't support Docker, We can do a trick to Install Docker Version which is supported in RHEL 7

  • Configure Docker Repository,
vim /etc/yum.repos.d/docker.repo

Add following configuration to file /etc/yum.repo.d/docker.repo

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

baseurl is the location, from where we are going to download docker

gpgcheck is to verify thee packages downloaded. In our case we are going to stop that by assigning gpccheck to 0

  • Install docker using the above command.
yum install docker -ce -nobest
  • Start and enable the docker services before preceding ahead.
systemctl start dockersystemctl enable docker
  • Pull the centos image from Docker Hub.
docker pull centos:latest

Create a Dockerfile

  • We just cloned the image, Let’s quickly create a Dockerfile, Basically Dockerfile is a set of Instructions from which we are going to build a Docker Image.
  • Docker Image — It’s like an ISO file. When we run docker image It convert into a process

Let’s write Instructions to create Docker Image, The Dockerfile contain the Base OS and the additional configuration We can just write instructions to Install Firefox a GUI application we are going to test, You can install any application

Additional Documentation to understand the Dockerfile

FROM centos:8RUN yum install firefox -y CMD /usr/bin/firefox

It’s a simple Dockerfile with a BaseOS centos:8 and We are going to install firefox and run firefox.

  • Build Docker Image from Dockerfile
docker build -tag <imagename:version> path-of-Dockerfile
  • run container

Launch the Container

  • To remove network isolation between the container and the Docker host we use host network, with which container can interact with Xserver in Host

— net=host

  • To share the DISPLAY environmental variable to the container.

— env=DISPLAY

  • Share hosts X server by creating container volume.

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

The command to run GUI container

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

Finally, we can see the firefox window pops out in Host Operating system.

Thanks for Reading

Let’s connect: LinkedIn

--

--