🐳Running GUI Applications inside Docker Containers🐳

rishabhsharma
3 min readMay 13, 2021

--

While the IT world is embracing Containers Technology primarily for Enterprise Server Applications, there is also a huge scope of Docker Containers impacting the Desktop and Development Environment.

Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers.

What are the issues while launching a GUI application?

Docker doesn’t come with GUI as there is are minimal use cases with it. So, if we want to run GUI applications on a docker container then we need a display, as the container is not connected to the display. We need to connect the display of Base-OS.

There can be two types of applications (usually services) that you can containerize

  • Applications that run as a Background Service (like a Database, webserver)
  • GUI Applications that run in the foreground (like firefox, jupyter notebook)

Foreground applications requires Xserver

X is an application that manages one or more graphical displays and inputs like (keyboard, mouse, webcam etc.) connected to the computer. X is the GUI protocol for Linux/Unix system.

So, Services communicate with the X-server to display a graphical interface and receive input from the user and the X-server clients can be on the remote system.

For a GUI Application to run, we need to have a XServer which is available as part of every Linux Desktop Environment, But within a Container we don’t have any XServer — so we will

  • Share the Host’s XServer with the Container by creating a volume

--volume="$HOME/.Xauthority:/root/.Xauthority:rw"

  • Share the Host’s DISPLAY environment variable to the Container

--env="DISPLAY"

  • Run container with host network driver with

--net=host

So let’s get started

First we have to start the docker service

# systemctl start docker
# systemctl enable docker
# systemctl status docker

Command to pull image

# docker pull centos:latest

Now let’s create a Dockerfile for creating docker image for GUI:

vim Dockerfile

cat Dockerfile

Use this Dockerfile to build an image with a GUI application for running jupyter notebook inside docker container

docker build -t jupyter-gui .

After this let’s launch the container from our created docker image jupyter-gui

# docker run --name jupyter --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" jupyter-gui:latest

Thanks for reading !!!😊✨ keep learning! keep growing!

--

--

rishabhsharma

AWS Certified ☁️ | PySpark | DevOps | Machine Learning 🧠 | Kubernetes ☸️ | SQL 🛢