Suppose you are building an application that requires user interface and pops up a window on running the script. And let’s say you want to run that script inside a docker container. Now, you might expect the docker container to run the GUI application for you and display the same on your screen. But using normal docker run commands, you won’t be able to see or interact with the UI application. You need to connect the display with the container in order to do so. In this article, we will discuss how to do exactly the same.
Here, we will see how to run a firefox instance inside a docker container and interact with it in your host machine. To do this, we need to first forward the X11 socket to the container so that the container can use it directly. We also need to forward the display environment variable. Even after doing this, it might fail because we have not yet set the permissions for the X server host. This might seem a hefty task at first. We will run you through all the steps in this article.
# Set ubuntu as base image
# Install dependencies
RUN apt-get -y update
RUN apt-get -y install xauth
RUN apt-get -y install firefox
#Expose a port number
# Run firefox
We have pulled ubuntu as a base image. After that, run the update on the newly formed ubuntu image. This will also form a new intermediate layer. Install xauth and firefox using the given commands. Xauth is a simple mechanism which would allow docker containers to access control of the X Servers also called the display servers. However, we need to manually add a randomly generated cookie for the session on which the X server is currently running.
docker run -ti --net=host -e DISPLAY -v /tmp/.X11-unix <IMAGE NAME> bash
Performing similar steps, you would be able to run any GUI application inside the docker container and display and interact with them on your screen.
I also install xeyes GUI app in centos version 7
First create the separate directory
# cd /gui
In this directory create the Dockerfile
# vim DockerfileFROM centos:7
RUN yum install xeyes -y
Now build the image by using the command
docker build -t xeyes .
Now you can able to see the docker image called xeyes latest by the command
Now finally write the command to run the container
docker container run -it --env="DISPLAY" --net=host xeyes
Now the xeyes GUI application is running