Skip to content

Instantly share code, notes, and snippets.

@Moosems
Last active October 17, 2024 12:50
Show Gist options
  • Save Moosems/138cfea6fc4e1967e4eae52bd96618ff to your computer and use it in GitHub Desktop.
Save Moosems/138cfea6fc4e1967e4eae52bd96618ff to your computer and use it in GitHub Desktop.
Tkinter Docker App Example
# Build the image
docker build -t dip .
# # Get IP address of Mac
IP=$(/usr/sbin/ipconfig getifaddr en0)
# # Print IP address
echo "IP: $IP"
# # Allow connections through XQuartz
/opt/X11/bin/xhost + "$IP"
# Run the container
docker run -u=$(id -u $USER):$(id -g $USER) -e DISPLAY=$IP:0 -v /tmp/.X11-unix:/tmp/.X11-unix:rw --rm -it dip
FROM ubuntu:20.04 as builder
FROM python:3.10.7
# Create a user to run the application
RUN apt-get update && \
apt-get -y install sudo
RUN useradd -ms /bin/bash docker && echo "docker:docker" | chpasswd && adduser docker sudo
# Allow sudo without password (add docker user to sudoers)
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# Switch to the docker user
USER docker
# Become root
USER root
# Set the working directory for the container
WORKDIR /home/docker
COPY . .
# Install PIL and tk dependencies
RUN sudo apt-get install apt-utils
RUN sudo apt-get update && apt-get install -y \
python3-pil \
python3-tk \
python3-pil.imagetk
# Install the dependencies
RUN pip install --upgrade pip; \
pip3 install -r requirements.txt
# Create /.local directory
RUN mkdir /.local; mkdir /.local/share; chmod 777 /.local; chmod 777 /.local/share
# Set the display port to avoid crash and to be able to use the container with a remote host
ENV DISPLAY=:0
# Run the application
CMD ["python3", "main.py"]
from tkinter import Tk, Label
from platformdirs import user_data_dir
from pathlib import Path
root = Tk()
root.title("Hello World")
root.geometry("400x200")
Label(root, text="Hello World").pack()
# Get the path to the user data directory
dir = Path(user_data_dir()) / "HelloWorld"
# Create the directory
dir.mkdir(parents=True)
root.mainloop()
@yahia-adam
Copy link

for linux user

#!/bin/bash

###### Build the image
docker build -t flaskapp .

###### Get IP address of the host
IP=$(hostname -I | awk '{print $1}')

###### Print IP address
echo "IP: $IP"

###### Allow connections to X server
xhost +local:docker

###### Run the container
docker run -e DISPLAY=$DISPLAY \
           -v /tmp/.X11-unix:/tmp/.X11-unix:rw \
           -v $HOME/.Xauthority:/root/.Xauthority:rw \
           --net=host \
           --rm -it flaskapp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment