Last active
October 17, 2024 12:50
-
-
Save Moosems/138cfea6fc4e1967e4eae52bd96618ff to your computer and use it in GitHub Desktop.
Tkinter Docker App Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
platformdirs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for linux user