GUI Application inside a Docker Container

Docker is a containerization tool that is used to run an application with its own environment dependencies making the container an isolated OS tailed just the run that one application. Docker is primarily used to run specific applications inside a container that do not need a GUI, like a Web Server or a Database Server.

But, we could perform some tweaking to the container to provide it the X Server which is used to run GUI, and also connect the display of the DockerHost to the Container.

note: DockerHost is the OS on which the Docker Engine is Running.

Project Architecture

This article covers launching a Graphical Application inside a Docker Container. We will be running Juypter Inside the Docker Container. We will be also mounting a directory of the DockerHost to a directory inside the container that will store the saved Jupyter Notebooks.

Getting Started

$ echo $DISPLAY
  • Confirm that hostnames or user names to the list allowed to make connections to the X server. If not perform the following command to add the user to the list of allowed connections for the X-server
$ xhost +

That’s it, we're good to go!

Creating a Project Directory

$ mkdir GUI_JUYPTER_CONTAINER/

Creating a Dockerfile

FROM centosRUN yum install firefox python3 -y && pip3 install jupyter && yum install passwd -y && useradd -u 1002 jupyter_user && echo redhat | passwd jupyter_user --stdinUSER jupyter_userWORKDIR /home/jupyter_userCMD ["jupyter","notebook"]

Building the Docker image

$ sudo docker build -t gui_container:v0.01 .

Creating a Directory to Store the Jupyter Files on DockerHost

$ mkdir GUI_JUYPTER_CONTAINER/saved_files/

Our Project Directory Looks like —

Running the Docker Container

Docker container does not provide us with support for Graphical Application by Default. Thus we need to tweak some parameters while running the Dockerfile.

Since we have to use the display of the DockerHost, we can use the host network to run the container. The drawback of using the host network is that the isolation of the container is compromised. But one of the advantages is that we could share the devices of the Docker host like Display with the container.

note: The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

Also, we need to pass the DISPLAY environment variable to the container so that it could recognize the Device. Generally, it’ll be :0 on a desktop PC or a laptop — referring to the primary monitor

Therefore, to sum up, we need a container with —

  • Name as jupyter_container [--name]
  • An interactive terminal [-it]
  • Mount the saved_files directory to the home directory of the jupyter_user inside the container (created using Dockerfile) [-v]
  • An Environment Variable “DISPLAY” [--env]
  • The host network [--net]
$ sudo docker run -it --name jupyter_container -e "DISPLAY" -v "$(pwd)"/saved_files:/home/jupyter_user  --net=host gui_container:v0.01

Checking the presence of Jupyter Notebooks on the DockerHost “saved_files” Directory.

We can check that the Welcome.ipynb creates and saved inside the container is mounted on the saved_files directory.

We have successfully Launched a GUI container inside a Docker Container and Saved the Jupyter Notebooks created inside the container on a directory on the DockerHost.

If you have any queries or suggestions— you can connect with me at:

Thank You!

Final Year Student, understanding the industrial approach and tools