Running GUI Applications on Docker in Linux
Last Updated :
21 Oct, 2020
Let’s say you are trying to build a UI application and deploying it as a Docker Container. If you want that UI application to display the user interface on your local machine while running the application inside the Docker Container, you will have to connect the display of the Docker Container with the display of your local machine. Let’s suppose you want to run Mozilla Firefox inside a Docker Container but you want to access the Firefox browser on your local machine. You can do this using some simple steps that we are going to discuss in this article.
Skimming through the entire process, you need to forward the X11 socket of your local Linux machine to the Docker Container for it to use directly. Along with this, you also need to forward the environment variable called display. After that, you need to set the permissions for the server host. Let’s run through these steps along with the code.
1. Creating the dockerfile
Create a dockerfile with the following code.
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install firefox
RUN apt-get -y install xauth
EXPOSE 8887
CMD firefox
The above dockerfile contains the sequence of instructions to create an Ubuntu base image, runs an apt update on it, installs Xauth and Firefox. It then exposes port 8887 and runs Firefox. Xauth simply allows Docker Containers to access the Display Servers.
2. Copying the Cookie to connect X Server Displays
On your local machine, get the cookie value using the following command.
xauth list

Authorization Cookie
Copy the output which would be of the form as shown below:
<username>/unix: MIT-MAGIC-COOKIE-1 f1fa006c1e51fa8386209f85c57947c4
3. Build the Docker Image
Create the Docker Image using the following command.
docker build -t <image-name> .

Docker build command
4. Run the Docker Container
Run the Docker Image using the Docker run command.
docker run -it --net=host -e DISPLAY -v /tmp/.X11-unix <image-name> bash

Docker run command
The Docker image is now built and the Container is started. It pops up an interactive Ubuntu bash.
5. Add the cookie to the list
You need to add the cookie copied in the previous step using the following command.
xauth add <cookie>
xauth list

Adding Cookie
The list command will verify that the cookie has been added successfully.
6. Run the Firefox Instance from the bash
To run a Firefox instance, simply type “firefox” inside the bash. You will find that the Firefox browser pops up on your local machine even though it is running inside the Docker Container. Using a similar process, you can run almost any user interface application inside Docker Containers and access it on your local machine.

Running Firefox Instance
To conclude, in this article we saw how to create a Docker Image using the dockerfile, build and run the image. We also discussed how to connect the display of the Docker Container to the display of the local machine and accessed a UI application running inside Container on the local machine.
Similar Reads
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
Running Docker Containers as Non-Root User
By default, Docker Containers run as Root Users. Now, if you are running applications inside Docker Containers, you have access to all the root privileges. This poses a great security threat when you deploy applications on large scale inside Docker Containers. Because if somehow your application get
2 min read
Tips to Manage Docker Containers using CLI
Before virtualization, the management of web servers and web applications was tedious and much less effective. Thanks to virtualization, this task has been made much easier. This was followed by containerization which took it a notch higher. For network engineers, learning the basics of virtualizati
13 min read
Running Commands Inside Docker Container
If you are working on an application inside the Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development ph
6 min read
Implementation of CI/CD in Java application(Linux) Using Shell and Docker Executor on GitLab
There are many executors available to implement CI/CD with GitLab Runner. However, Shell and Docker are more popular among them, and we can easily configure a repository with these runners. These runners can be chosen based on the requirements and availability of the resources. This article mainly f
6 min read
Setup Web Server Over Docker Container in Linux
Before setting up a server we must know the real meaning or definition of a server. So, the Server is a program that provides the client with any kind of services. For example, a web server provides our websites, a database server provides us data. This means every server has work to do and for ever
2 min read
Mounting a Volume Inside Docker Container
When you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers
10 min read
Exploring a Docker Container's Filesystem in Linux
There are times during our development process when we need an in-depth exploration of the filesystem of the docker container that we are using. But how to do that? In this article we are going to look at 6 ways that are used for the above purpose. If you are not familiar with the basic commands use
5 min read
How To Fix "Bash: Docker: Command Not Found" In Linux
Docker has become an essential tool for developers and system administrators to manage and deploy applications efficiently. However, encountering the error message "Bash: Docker: Command Not Found" can be frustrating, especially when you're trying to work with Docker containers. Here, we'll explore
4 min read
How to Install and Configure Docker in Ubuntu?
Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read