Empowering Docker using Tkinter GUI | by Kasper Schøn Henriksen | Towards Data Science

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.
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:
Create very simple application using Tkinter.Create a simple Docker image and run a Tkinter application in the Docker container.Simplify Docker run command using shell script. (Optional)Allow continuous development of GUI. (Optional)
Create very simple application using Tkinter.
Create a simple Docker image and run a Tkinter application in the Docker container.
Simplify Docker run command using shell script. (Optional)
Allow continuous development of GUI. (Optional)
This article has only been tested with Linux and may not work for other OS. You should also already have installed Docker.
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.pyDockerfilerun.sh (optional)
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.
To create this simple application, the code below is utilized.
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/tkinter_app.py with Python.
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_in_docker.
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 pastingdocker 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.
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
In our scenario, we have a single file for the application tkinter_app.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 tkinter_app.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 commanddocker 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.
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.
This is my first article on Medium and feedback is therefore more than appreciated.