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.
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.
- Confirm that your DOCKERHOST has a value of the DISPLAY environment variable. Mostly it’ll be :0 be there could be exceptions. Depending on your displays.
$ 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
It’s always good to have a new directory for every project.
$ mkdir GUI_JUYPTER_CONTAINER/
Creating a Dockerfile
We will be using centos image pulled from the docker hub to build Docker Image. In order to run Jupyter Notebook inside the container will need to install a web browser and python3 inside the container.
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
Let’s create a volume to store all the Jupyter files so that our files are persistent even after the container is terminated.
$ mkdir GUI_JUYPTER_CONTAINER/saved_files/
Our Project Directory Looks like —
Running the Docker Container
Here, we will be using our gui_container:v0.01 image to run a 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:
Prathamesh Mistry - MLOPS Summer Intern - LinuxWorld Informatics Pvt Ltd | LinkedIn
View Prathamesh Mistry's profile on LinkedIn, the world's largest professional community. Prathamesh has 4 jobs listed…