Running GUI Application inside Docker Container

Tharakramdasari
3 min readMay 4, 2021

--

Hey Readers , in this article I am gonna show you how to launch a GUI application inside Docker Container.

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

With Docker we can launch any OS in seconds. Generally we interact with docker using CLI(Command Line Interface).

Normally we have two types of applications which is designed to be run either background or Foreground docker. where GUI application works on Foreground. For launching the GUI application we have to connect the display of the container to the base os. For running the Foreground application always required X-server which mainly present in the linux system For that base of the docker engine is compulsory to be Linux system.

Before launching container Configure RedHat 8 OS repository with the help of ansible automation tool

Here is the playbook for configuring Yum

- hosts: all
vars:
- dvd_dir: "/dvd1"

tasks:
- file:
state: directory
path: "{{ dvd_dir }}"
- mount:
src: "/dev/cdrom"
path: "{{ dvd_dir }}"
state: mounted
fstype: iso9660

- yum_repository:
baseurl: "{{ dvd_dir }}/AppStream"
name: "dvd1"
gpgcheck: no
description: "Yum repo 1"
- yum_repository:
baseurl: "{{ dvd_dir }}/BaseOS"
name: "dvd"
gpgcheck: no
description: "Yum repo 2"

Created a Dockerfile to build Docker image

FROM centos:latest
RUN yum install firefox xa
RUN yum install libcanberra-gtk2 -y
RUN yum install PackageKit-gtk3-module -y
CMD ["/usr/bin/firefox"]

Build an image using Dockerfile using the following command

docker build -t <imagename> .

Check whether it has build image or not

It built image names task26 . Now let’s launch a container using above image task26 before going to create a container we need to pass some arguments

Running the container with the network host and with the above build image.

--net=host

Passing the envirnoment variable DISPLAY with the value DISPLAY which is passing the base display

-e DISPLAY=$DISPLAY

Mounting the X11 socket volume residing in /tmp/.X11-unix in the base os and mounting to the /tmp/.X11-unix in the container.

-v /tmp/.X11-unix:/tmp/.X11-unix

To launch the container to run the gui application inside the docker container with the following command

docker run -it --net=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix task26

Finally it launched firefox which is GUI application inside docker

Thanks for reading this article. Hope you learned something new

--

--

Tharakramdasari
Tharakramdasari

Written by Tharakramdasari

CSE Student at IIIT ONGOLE Learner at ARTH School of Technologies AWS || Docker || Ansible || Kubernetes CKA & CKAD || Hadoop || GCP || Machine Learning || Git

No responses yet