Running GUI applications on docker:

Running GUI applications on docker:

We know that docker is a tool which is used to create an environment to run an application. Now, here is a scenario what if one wants to run a GUI(Graphical User Interface) application on docker how should we do that? This article will give you idea to run a GUI application on the docker and make it available on the docker host(The machine where the docker server is running).

Building the image:


  • Firstly to run an application we need to install the application hence first we need to install the application on the container.
  • For this we can either launch the container and then install or create an image with the required environment and then launch the container(I prefer the second method in this way we can make the container to run only to the life of the program).
  • Here environment that we create should have the GUI application we want to launch.
  • Hence we need to install the GUI application on the container. And we also need to have a base operating system for the container to install the GUI application.

    • We are using the base operating system as centos (one may use operating system of their choice)
    • Then we need to install the required applications. I am going to install firefox(one may install application of their choice).
  • To create the image we are going to create a Dockerfile in which we give the base operating syste m and the installation procedure. For the above mentioned configuration Dockerfile will look as follows:

FROM centos:latest
RUN yum update -y && yum install firefox -y
CMD firefox
image.png
Building the image
  • Now after creating the Dockerfile we need to build the image using the command docker build . -t <image_name>:<tag> in the directory where the Dockerfile is created.

    Where <image_name> is name of image and <tag> is the tag for the image. If the image is created successfully then we can launch the container.

Configuring the Docker host:


  • Before launching the container with GUI application we need to do some configuration on the docker host.
  • As we need to use the GUI of the docker host and launch then gui application for this we need a display on the docker host, if the docker host is linux as linux has xserver which is the environment for display hence we can directly use the xserver of the docker host.
  • But if the docker host's operating system is windows then we need to install the xserver, there are tools available to install xserver on windows one of them is xming we can install xming and then launch the xserver.
  • On linux the xserver can only be used by the authorized clients which are the application that only run locally. Now we need to allow the outside applications for this we need to use the command xhost + which will allow the clients from any host to connect to the xserver. Xming by default will allow clients from anywhere.

Launching the container:


  • Now that the xserver is configured we can launch the container. We know that to launch the container we need to use the command docker run.
  • To run container in background we use -d option. We don't need terminal option as we dont need to access the terminal if there is a need to access the terminal one can use -t option to get the terminal and -i option to make it iteractive terminal.
  • In linux we need to use the command docker run -d -v /tmp/.X11-unix:/tmp/.X11-unix -e "DISPLAY" <image_name>:<tag> where DISPLAY is the environmental which contains the DISPLAY and we mount the /tmp/.X11-unix folder in the container in the same location to say where the xserver is to the docker container.
image.png
Status of xhost before running xhost + command
image.png
Output of xhost + command
  • If the xserver is xming then it will provide an IP address and the display number which can be given as input in the DISPLAY environmental variable for example docker run -d -e DISPLAY="<ip_address>:<display_number>" <image_name>:<tag>

By following the above procedure we can successfully launch a GUI application

Final Output:


image.png