1. Home
  2. Linux
  3. How to run gui apps in linux containers

How to Run GUI apps in Linux Containers

Ever thought about running GUI applications from a container on your Linux system? It’s entirely possible! Whether you’re using Fedora and require an Ubuntu application, or you’re curious about running an Arch Linux program on Ubuntu, you’re in luck. This guide will walk you through the steps to seamlessly run GUI apps in containers across various Linux distributions.

The Hero image for Docker GUI.

How to Set Up X11 Forwarding on the Host System

To enable X11 forwarding from a Docker container, you first need to install the xhost tool on your Linux system. Begin by opening a terminal window on your Linux desktop and follow the command instructions below.

Ubuntu/Debian

For Ubuntu or Debian users, install the X11 forwarding tool using the apt install command:

sudo apt install x11-xserver-utils

Arch Linux

On Arch Linux, use the pacman -S command to install the xorg-xhost package:

sudo pacman -S xorg-xhost

Fedora

For Fedora, use the following dnf install command to install the “xorg-x11-server-utils” package:

sudo dnf install xorg-x11-server-utils

OpenSUSE

On OpenSUSE, install the X11 forwarding tool using the zypper in command:

sudo zypper in xorg-x11-server-extra

After installing the software, run the xhost + command. This command reduces the security in X11, allowing you to display programs from your container on your host system directly.

xhost +

Writing a Dockerfile

To create a custom container, you must write a Dockerfile. Start by creating a folder named “docker-app-project” using the mkdir command:

mkdir -p docker-app-project

Then, enter the folder using the cd command:

cd ~/docker-app-project

Inside the folder, create a new Dockerfile using the touch command. The Dockerfile serves as an instruction manual for Docker to build your image:

touch Dockerfile

Open the Dockerfile in the Nano text editor for editing, and paste the example code below. In this example, we’re building an Ubuntu image capable of running the X11 app xeyes.

The xeyes Dockerfile used to build the custom image.

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

Save your changes with the Ctrl + O command. Finally, build the image using the docker build -t command:

sudo docker build -t xeyes .

Deploying the container

Now that you’ve built your base container image out of the Dockerfile, it is time to deploy it to Docker as a container. Doing this isn’t difficult at all. To start the container, you need to run the docker run command.

Docker running the GUI Xeyes application from the container to the host system.

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

Running this command will execute the xeyes application from the container on your Desktop and display it on your screen.

Making your own GUI container app

In this guide, we covered setting up a basic app like Xeyes, however, you can make your container launch any number of GUI apps. In this section of the guide, we’ll show you how you can write your own.

To start, create a new “Dockerfile” using the touch command.

touch Dockerfile

After creating your Dockerfile, open it up in the Nano text editor, and add the FROM ubuntu:latest command. This command will tell Docker that the Docker image must be based on Ubuntu’s latest release.

FROM ubuntu:latest

After adding the FROM ubuntu:latest command, it is time to add a RUN command. This command will execute commands inside of the image during the build process.

RUN apt update && apt upgrade -y &&

The first part of the RUN command instructs the Ubuntu Docker image to update and install upgrades on Ubuntu. Next, add the GUI program you wish to install into Ubuntu. However, be sure that it is available in Ubuntu’s default software repositories.

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

When you’ve added your RUN command, add the CMD at the end of the “Dockerfile.” This command will execute your program from the container to the host machine. Please note that your program may not run if you do not know the launch command. Do a bit of research to find out what command launches your program via the terminal.

CMD MY_PROGRAM_HERE

After setting up your  CMD section of the “Dockerfile,” it should look like this:

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

From here, save with the Nano text editor by pressing Ctrl + O on the keyboard, and exit with Ctrl + X. You can then build your custom Docker image using the following docker build command.

sudo docker build -t MY_PROGRAM_HERE .

When everything is built, you can execute the newly built image as a Docker container on your system by running the following docker run command.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.