Running GUI Apps on a Docker Container
Containerization and the GUI
Before we launch GUI on docker, let's understand how GUI works on any system. What provides a display interface or a graphical interface between the system and the user is a client/server windowing system known as the X Window System, and sometimes X11 or even X.
It is a dedicated terminal that displays the windows and handles input devices such as keyboards, mice, and touchscreens. The clients are applications.
To run GUI apps on Docker we need to use X11 of our host.
Running GUI Containers on docker in 5 easy steps
Step 1: Install Docker
To be able to launch a container you need to first have Docker installed. On your host OS (preferably Linux) get Docker installed and then move on to the next step.
Step 2: Pull an image
Download a container image of your choice from Docker Hub. In this blog I am using CentOS image.
To pull the image from Docker Hub type
docker pull centos
docker images
This command will show you the images you have in your local system.
Step 3: Create the container
Method 1:
Launch a container using docker run command
docker run -dit --name centos_gui --env=’Display’ --net=host centos:latest
Here, the run subcommand is used to launch the container, -i to make it interactive, -t to get the terminal, and -d to daemonize it. We use --env to set the environment variable DISPLAY and --net to connect the container to the host network.
Method 2:
Set the DISPLAY environment variable on the host OS with your network:
set-variable -name DISPLAY -value YOUR-IP:0.0
And then finally launch the container using
docker run -dit --env=DISPLAY=$DISPLAY
Here we have manually set the environment variable.
docker ps
This command shows running containers
Step 4: Install GUI application inside the container
Attach the container to go inside the container’s terminal. To do so, type:
docker attach centos_gui
Now you are inside the container’s terminal.
Next, install a GUI application using a package manager. In our case since we have CentOS, we will be using the yum command.
yum install firefox
Step 5: Running the application
As we have already specified the DISPLAY environment variable while launching the container, now all we have to do is just run the app normally.
firefox
In a while, the GUI of the app will show up something like this.
We have successfully launched firefox GUI over a docker container!
Feel free to contact on my LinkedIn.