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

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:
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:
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:
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:
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
You should read it
- How to safely check desktop applications with Docker
- 6 reasons to use Docker virtualization software
- How to Containerize a Nest.js Application Using Docker and Docker Compose
- Containerize Go App with Docker
- How to run Docker on Raspberry Pi
- How to use Docker Container
- Docker best practices you need to know
- Docker Hub is used by hackers to spread Cryptojacking malware
- 10 Best Docker Alternatives 2022
- Docker and .NET APIs: Simplifying Deployment and Scaling
- 5 useful tips to learn Docker in 2018
- Common commands in Docker
Maybe you are interested
6 Legal Ways to Pay Less for Spotify Premium The Best Mental Health Activities for Kids Summary of Tik Tok video editing apps on Android, iOS Instructions for viewing battery charging time on iOS 26 No need to update individual apps? Microsoft wants Windows Update to handle it all How to create borders in PowerPoint