Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Run GUI-Based Applications in Docker

Learn how to run gui-based applications in docker. Follow clear steps, practical tips, and troubleshooting advice for common problems. Read the complete guide.

Table of Contents

This practical guide explains how to run gui-based applications in docker. Follow the steps in order, review the requirements first, and use the troubleshooting notes when a setting or option does not work as expected.

Why run GUI applications in Docker?

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

  • 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.
  • 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.
  • Containers make it easy to test new applications or debug something. You can run, pause, or delete them without affecting your host machine.
  • 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.
  • 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

Running GUI applications in Docker screenshot

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:

Enable Docker Service screenshot

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 Confirm 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:

Build Docker image screenshot

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:

Launch Docker Container with GUI support enabled screenshot

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:

Launch Docker Container with GUI support enabled screenshot

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

Disconnect Docker from the X server screenshot

FAQ

Why run GUI applications in Docker?

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

What should you know about 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:

What should you know about enable Docker Service?

Now, start the Docker service with the following command:

What should you know about 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:

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.