Running GUI Applications in Docker: A Comprehensive Guide for Linux Users

Have you ever considered the possibility of executing graphical user interface (GUI) applications from a containerized environment on your Linux machine? Whether you’re operating on Fedora and wish to run an application native to Ubuntu, or you’re interested in executing an Arch Linux program while using Ubuntu, this guide is your key to success. We will provide step-by-step instructions to effortlessly run GUI applications in containers across a variety of Linux distributions.

Setting Up X11 Forwarding on Your Host System

To facilitate X11 forwarding from a Docker container to your Linux host, the initial step involves installing the xhost utility. Open a terminal on your desktop and follow the appropriate commands based on your Linux distribution.

Ubuntu/Debian

For users of Ubuntu or Debian, use the following command to install the X11 forwarding tool:

sudo apt install x11-xserver-utils

Arch Linux

If you’re on Arch Linux, install the xorg-xhost package with:

sudo pacman -S xorg-xhost

Fedora

Fedora users can utilize this command to install the “xorg-x11-server-utils” package:

sudo dnf install xorg-x11-server-utils

OpenSUSE

On OpenSUSE, install the X11 forwarding utility by running:

sudo zypper in xorg-x11-server-extra

Once the installation is complete, execute the following command to allow your container to communicate with your host display:

xhost +

This command opens up some security, allowing applications from the container to be displayed on your host system.

Crafting Your Own Dockerfile

To create a customized container, you’ll need to write a Dockerfile. Start by creating a new directory for your application:

mkdir -p docker-app-project

Next, navigate into the directory:

cd ~/docker-app-project

Inside this folder, create a new Dockerfile that will instruct Docker on how to build your image:

touch Dockerfile

Open the newly created Dockerfile using your preferred text editor. Below, we provide an example that builds an Ubuntu image capable of running the X11 application xeyes.

FROM ubuntu:latest
RUN apt-get update && apt-get install -y x11-apps
CMD xeyes

After pasting the code into the file, save your changes (for Nano: Ctrl + O) and exit the editor. Now, build your Docker image using:

sudo docker build -t xeyes .

Deploying Your Container

With your base container image successfully created from the Dockerfile, it’s time to deploy it. Start the container using the following command:

sudo docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:ro xeyes

Executing this command will launch the xeyes GUI application inside your container, displaying it right on your desktop.

Creating Your Own GUI Container Application

While this guide demonstrates the setup of the xeyes application, you can easily modify it to run any GUI app of your choice. Here’s how to craft your personalized container app.

First, create a new Dockerfile:

touch Dockerfile

Edit it with your text editor, starting with the following line to base your image on the latest Ubuntu release:

FROM ubuntu:latest

Then, add a RUN command to update the package list and install GUI applications available in Ubuntu’s repositories:

RUN apt update && apt upgrade -y && apt install x11-apps MY_PROGRAM_HERE

Finally, specify the command to launch your application with CMD:

CMD MY_PROGRAM_HERE

Your finalized Dockerfile should resemble the following:

FROM ubuntu:latest
RUN apt update && apt upgrade -y && apt install MY_PROGRAM_HERE
CMD MY_PROGRAM_HERE

Save your Dockerfile (for Nano: Ctrl + O), then build your custom Docker image:

sudo docker build -t MY_PROGRAM_HERE .

Once the build is complete, execute your container with:

docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:ro MY_PROGRAM_HERE

Running GUI applications in Docker containers opens up remarkable possibilities for Linux users across various distributions. By following the above steps, you’re now equipped to harness Docker’s containerization power to run GUI applications seamlessly, enriching your desktop experience.

By

Tech journalist and digital trends analyst, Alex Reynolds has a passion for emerging technologies, AI, and cybersecurity. With years of experience in the industry, he delivers in-depth insights and engaging articles for tech enthusiasts and professionals alike.