How to Run GUI-Based Applications in Docker

Docker is typically used for server-side and command-line applications. However, with the right setup, you can also run GUI-based applications inside containers. These containers can include GUI libraries and rendering engines, allowing applications to run in a secure, isolated environment. This approach simplifies the development, testing, and deployment of GUI applications across multiple machines or operating system environments. This article will show you how to run GUI applications inside Docker containers with minimal setup.

 

Why run GUI applications in Docker?

Here are the main reasons why running GUI applications in Docker can be beneficial:

 

  1. When you run a GUI application in Docker, everything the application needs, like libraries and settings, is packaged inside the container. This keeps your main system from getting cluttered or conflicting.
  2. Using Docker means your application will work the same on every machine. Whether you're developing, testing, or sharing with others, the environment remains consistent.
  3. Containers make it easy to test new applications or debug something. You can run, pause, or delete them without affecting your host machine.
  4. Docker allows you to run Linux GUI applications on non-Linux systems. Docker does this by using display sharing tools like XQuartz or VcXsrv, so no virtual machine is required .
  5. Unlike traditional VMs, Docker containers use fewer system resources. They start up faster and run smoother, even for GUI-based applications.

 

Running GUI applications in Docker

To run a GUI application in Docker, you must first ensure that Docker is installed on your Linux system. You can check this by running the following command:

docker --version

How to Run GUI-Based Applications in Docker Picture 1

If this command returns a version number, it means Docker is installed and running. If not, you will likely see a "command not found" error.

Once Docker is properly set up, you can move on to the next steps.

Enable Docker Service

Now, start the Docker service with the following command:

sudo systemctl start docker

To check if the Docker service is running correctly, run:

sudo systemctl status docker

The output confirms that the Docker service is up and running without any issues:

How to Run GUI-Based Applications in Docker Picture 2

 

Set up project directory and Dockerfile

Let's create a directory called "dockerGUI" where we will store all the Docker related files for running GUI applications:

mkdir dockerGUI

Now, let's navigate to this directory to make sure that all the subsequent files we create or modify will be saved in the dockerGUI directory:

cd dockerGUI

Create a new file called dockerGUIFile to define the Docker image configuration:

nano dockerGUIFile

Now, paste the following lines of code into the dockerGUIFile:

FROM jess/firefox ENV DISPLAY=:0 CMD ["firefox"]

The above code tells Docker to use the prebuilt Firefox image and set up a display environment so that the GUI can appear on the host system. Furthermore, it ensures that Firefox is automatically launched when the container is running.

Note : To try a different application, simply change the image and command in the Dockerfile. For example, to run Gedit, you can use the official Ubuntu image and install the application during the build process as follows:

FROM ubuntu RUN apt-get update && apt-get install -y gedit ENV DISPLAY=:0 CMD ["gedit"]

Build Docker image

Now that the Docker configuration is set up in the dockerGUIFile, let's build the Docker image using the following command:

sudo docker build -t myfirefox:1 -f dockerGUIFile .

This command builds a Docker image from dockerGUIFile, named myfirefox with tag 1 and using the current directory as context:

How to Run GUI-Based Applications in Docker Picture 3

 

Launch Docker Container with GUI support enabled

Now, enable GUI support for Docker containers by running the following command:

xhost +local:docker

The output confirms that local clients (such as Docker containers) running on the system are now allowed to connect to the X server:

How to Run GUI-Based Applications in Docker Picture 4

Now, run the container with the following command to launch Firefox with GUI support on your host system:

docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix myfirefox:1

As a result, this command connects the container to the display, allowing Firefox to open on your desktop like a native application:

How to Run GUI-Based Applications in Docker Picture 5

Note : Using the --rm option causes Docker to automatically delete the container after the application closes, keeping your system clean and preventing it from showing up in docker ps -s .

Disconnect Docker from the X server

After you're done using the GUI application, you should close X server access for security reasons:

xhost -local:docker

How to Run GUI-Based Applications in Docker Picture 6

4 ★ | 1 Vote

May be interested