Using a GUI application on a Docker Container
What is a GUI application?
GUI or the Graphical User interface is an interface that includes graphical elements such as symbols/icons, windows and buttons.
Apart from the fact that it provides users with an intuitive and easy-to-use interface and immediate visual feedback, a GUI also saves time editing configuration settings manually, helps prevent mistakes that can easily happen when editing config files manually,allows for easy memorization of tasks and much more…
A GUI application is a software program that interacts with the user through graphical means and allows interaction through clarity and control.
What is docker?
If you know the commands, a Command Line Interface can be a lot faster and efficient than any other type of interface and also requires less memory to use in comparison to other interfaces.
Due to these reasons a docker container, by default, operates on a CLI based environment.
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.
Containers are a form of operating system virtualization.
Why do we need to deploy GUI applications on Docker Containers?
There can be many reasons so as to why you would want to use a GUI application on top of docker container such as trying to build a UI application and deploying it as a Docker Container or needing a GUI application to perform a certain task.
Our task today includes deploying a GUI application on a docker container. For this task we are using Redhat8 (RHEL8) as the base OS and CentOS as the container image.
We will be using firefox browser, gedit text editor and Jupyter notebook IDE as GUI applications for this task
NOTE: This article includes the simplest method for deploying a GUI application on top of a docker container. In some advanced cases you may need to utilize certain more tools or commands, however for our current task this method works much more than enough.
Following is the step by step instruction to perform this task:
STEP 1: Create a Dockerfile in the baseOS (RHEL8 in our case)
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is essentially the build instructions to build the image.
You may to create a new directory to store this docker file, to create a directory use the command and use cd command to enter directory.
mkdir <file_name>
Now using the vim editor (any text editor of your preference) create a Dockerfile
vim Dockerfile
In the vim editor, press i to enter in insert mode and then write exact lines and save the file by pressing esc key then :wq and then enter.
The first part is the FROM command, which tells us what image to base this off of.
The next is the RUN command. This is what runs within the container at build time.
STEP 2: Enable Docker
NOTE: If you have not installed docker by now and need help, I recommend going through the step 1 of this post where I have explained how to load Machine Learning project on a docker container
systemctl enable docker
This enables the docker services on the system
systemctl status docker
This command is used to check the current status of the docker services
STEP 3: Build a new docker container with the dockerfile configurations
After starting the docker services you need to build a new docker image while the using the dockerfile configurations. This can be done using the docker build command. Its synatx is
docker build [OPTIONS] PATH | URL | .
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL .
docker images
This command is used to list the current images in the docker
docker build -t mygui:v1 .
Here I created a new docker image with name mygui and tag v1
NOTE: The full stop is also a part of the syntax
This step may take some time as it will install all the specified packages (firefox and gedit) along with their dependencies.
STEP 4: Create a docker container from the image just created
NOTE: As a docker container is CLI based by default so it by default does not have any environment to display the GUI application. So we need to provide it an environment (DISPLAY from host OS) and an internet connection.
So to create a container we use command :
docker run -it — name guiapp mygui:v1
Now we also need to add DISPLAY environment and network
docker run -it — name guiapp — env DISPLAY — net host mygui:v1
Here we can see a new container is created and is running however to display output it is still using the baseOS display panel.
Now check if the firefox and gedit packages have been installed or not.
For this we use the rpm command (Redhat Package Manager)
rpm -q firefox
rpm -q gedit
Here -q is used to Query for the program
STEP 5: Run the GUI applications
To run firefox directly use the following command:
firefox
With similar method you can also use even IDE’s inside a container, one such example being that of Jupyter notebook. For a jupyter notebook you need to either add
RUN yum install python3
in the dockerfile you created or you can even use
yum install python3
while you are running the container to download the python3 onto the container. After this you just need to install jupyter library using the pip3 command
pip3 install jupyter
and you are ready to use Jupyter notebook on your container.
NOTE: You will need to provide Jupyter notebook access to run on the root user
jupyter notebook — allow -root
Congratulations! You successfully learned how to deploy or run GUI applications on top of a docker container.