The world’s leading publication for data science, AI, and ML professionals.

Empowering Docker using Tkinter GUI

GUI through Docker is crucial for numerous applications.

Photo by Borderpolar Photographer on Unsplash.
Photo by Borderpolar Photographer on Unsplash.

In several cases, it can be useful to create a Graphical User Interface (GUI) that can be accessed in Docker, for instance when developing machine learning applications where visualizing image and video is desired. However, several steps are required to be able to view the GUI at the host machine. In this article, we will use Tkinter to create a simple GUI in Python which can be accessed through Docker.


Table of Content

In this article, we will cover several steps. The first two steps are necessary to run a Tkinter GUI application in Docker. The other steps are optional and are used to gain extra information of how to enhance the development phase using Tkinter and Docker. The steps are:

  1. Create very simple application using Tkinter.
  2. Create a simple Docker image and run a Tkinter application in the Docker container.
  3. Simplify Docker run command using shell script. (Optional)
  4. Allow continuous development of GUI. (Optional)

Prerequisites

This article has only been tested with Linux and may not work for other OS. You should also already have installed Docker.

Directory Structure

Below, an overview of the directory structure is presented. These files will be used for this article and you can therefore create them beforehand.

app/
├─ tkinter_app.py
Dockerfile
run.sh (optional)

Tkinter

Since the goal of this article is to demonstrate how a GUI can be accessed through Docker, a simple window is created as presented below. This GUI contains text and a button to quit the application.

Simple Tkinter application.
Simple Tkinter application.

To create this simple application, the code below is utilized.


Docker

Afterwards, a Dockerfile should be created and contain the following code. In this Dockerfile, we set the base of the Docker image to a slim version of Python 3.8.12. Afterwards, we update the package information and install Tkinter. The last two lines run the command _/app/tkinterapp.py with Python.

Build Docker Image

In the same directory as the Dockerfile, the following command should be executed to build the Docker image:

docker build -t tkinter_in_docker .

The command above builds a Docker image with the name _tkinter_indocker.

Running Docker Container

Now that the Docker image has been built, we can run a Docker container of the Docker image. To do this, it requires permission granting, which can have some safety issues. In this article, we will go with a safe approach. However, other approaches exist which are even safer, which are covered here. In this article, we will input our own user credentials by using the following command:

# Read the above before pasting
docker run -u=$(id -u $USER):$(id -g $USER) 
           -e DISPLAY=$DISPLAY 
           -v /tmp/.X11-unix:/tmp/.X11-unix:rw 
           -v $(pwd)/app:/app 
           --rm 
           tkinter_in_docker

In the above, –u is used to set the name of the user, –e to set the display, the first -v creates a volume in X11-unix to provide display, the second -v creates another volume where the Docker container can access our Tkinter application, and lastly we use –rm to automatically remove the container after usage.


Simplifying Docker Run using Shell Script (Optional)

The previous complex Docker run command as presented above, can be simplified by creating a simple bash file as following:

To make the shell script executable, you can run the command below:

chmod +x ./run.sh

Now you will be able to run it simply by using the command:

./run.sh

Continuous Development of GUI (Optional)

In our scenario, we have a single file for the application _tkinterapp.py and __ there is therefore little to no reason for running it manually inside a Docker container using Python. However, for larger applications it might be desired to run individual scripts within the Docker environment instead of running the same one each time.

To allow continuous development, follow the next steps:

  • Comment line 11 and 12 in your Dockerfile to prevent Docker from automatically running _tkinterapp.py.
  • Use the following Docker build command to rebuild the image with the new changes:
docker build -t tkinter_in_docker .
  • To run and interact with the Docker container, use the following command:
# Read "Running Docker Container" before running this command
docker run -u=$(id -u $USER):$(id -g $USER) 
           -e DISPLAY=$DISPLAY 
           -v /tmp/.X11-unix:/tmp/.X11-unix:rw 
           -v $(pwd)/app:/app 
           --rm 
           -it 
           tkinter_in_docker 
           /bin/bash

The above command is based on the previous Docker run command. -it is added to get an interactive terminal in the Docker environment, and /bin/bash is added to run it using Bash. If desired, you can replace the above command with the command in the shell script, which was previously covered.

  • Navigate to the application directory which is located in /app/.
  • Use the command to run the application:
python tkinter_app.py

Since we have setup a shared volume through our Docker run command, we are now able to locally use an IDE and continuously change our application without the necessity of restarting the Docker container.

Note: To detach the Docker container (exit the docker container), press Ctrl+D.


Conclusion

In this article you developed a simple GUI application using Tkinter which you were able to run through Docker. This is a powerful skill to acquire, especially within the field of data science, due to the vast possibilities of running interactive isolated GUI environments. Now you should be able to further develop your own application.

Thanks for reading

This is my first article on Medium and feedback is therefore more than appreciated.


Related Articles