From b0ebf88db0f4a1a9925c7d48b9f7cbf3a9beff4f Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:28:23 -0400 Subject: [PATCH 1/6] Improve Docker build Added a .dockerignore file to exclude unnecessary files from Docker context. Refactored Dockerfile to use multi-stage builds, pip cache, and offline wheel installation for faster and more reliable builds. Updated docker-compose.yml to mount .env as read-only. --- .dockerignore | 16 +++++++++++++ Dockerfile | 56 +++++++++++++++++++++++++++++++++------------- docker-compose.yml | 4 ++-- 3 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6347c08 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +.git +.gitignore +__pycache__/ +*.pyc +*.pyo +*.pyd +.build/ +dist/ +.wheels/ +.cache/ +.env +*.log +.mypy_cache/ +.pytest_cache/ +.vscode/ +.idea/ diff --git a/Dockerfile b/Dockerfile index dc71b68..e270ad6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,44 @@ -#!/bin/sh -# Build Stage +# syntax=docker/dockerfile:1.6 + + +# Build stage FROM python:3.12 AS builder WORKDIR /code -ADD . /code -RUN pip install "fastapi[standard]" -RUN pip install redis -RUN pip install sqlalchemy -RUN pip install pymysql -RUN pip install "sentry-sdk[fastapi]" -RUN pip install cryptography -RUN pip install pyinstaller -RUN pyinstaller -F main.py - -# Runtime + +# Use --mount=type=cache to cache pip downloads between builds +COPY requirements.txt /code/requirements.txt +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ + python -m pip install --upgrade pip wheel \ + && pip wheel -r requirements.txt -w /wheels + +# Coyp source code +COPY . /code + +# Install dependencies from wheels cache (offline installation) +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ + pip install --no-index --find-links=/wheels -r requirements.txt + +# Run PyInstaller to create a single executable +RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ + pip install --no-index --find-links=/wheels pyinstaller \ + && pyinstaller -F main.py + + +# Runtime stage FROM ubuntu:24.04 AS runtime WORKDIR /app -COPY --from=builder /code/dist/main . -EXPOSE 8000 -ENTRYPOINT ["./main"] + +# Install runner dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Copy the built executable from the builder stage +COPY --from=builder /code/dist/main /app/main + +# Health check +# HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ +# CMD curl -fsS http://localhost:8080/health || exit 1 + +EXPOSE 8080 +ENTRYPOINT ["/app/main"] diff --git a/docker-compose.yml b/docker-compose.yml index a800c67..cafa8e2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: volumes: - ./cache:/app/cache - ./dict:/app/dict - - ./.env:/app/.env + - ./.env:/app/.env:ro restart: unless-stopped depends_on: - tunnel @@ -33,4 +33,4 @@ services: restart: unless-stopped command: tunnel --no-autoupdate run environment: - - TUNNEL_TOKEN=${TUNNEL_TOKEN} \ No newline at end of file + - TUNNEL_TOKEN=${TUNNEL_TOKEN} From 26ca2aad5df978f56478ab6074e7385d9dfb82c5 Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:31:35 -0400 Subject: [PATCH 2/6] fix build --- Dockerfile | 7 +++++-- requirements.build.txt | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 requirements.build.txt diff --git a/Dockerfile b/Dockerfile index e270ad6..1e57072 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,16 +7,19 @@ WORKDIR /code # Use --mount=type=cache to cache pip downloads between builds COPY requirements.txt /code/requirements.txt +COPY requirements.build.txt /code/requirements.build.txt RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ python -m pip install --upgrade pip wheel \ - && pip wheel -r requirements.txt -w /wheels + && pip wheel -r requirements.txt -w /wheels \ + && pip wheel -r requirements.build.txt -w /wheels # Coyp source code COPY . /code # Install dependencies from wheels cache (offline installation) RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ - pip install --no-index --find-links=/wheels -r requirements.txt + pip install --no-index --find-links=/wheels -r requirements.txt \ + && pip install --no-index --find-links=/wheels -r requirements.build.txt # Run PyInstaller to create a single executable RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ diff --git a/requirements.build.txt b/requirements.build.txt new file mode 100644 index 0000000..fb36633 --- /dev/null +++ b/requirements.build.txt @@ -0,0 +1 @@ +pyinstaller==6.10.0 From a684202141a65e0df25826f823e45c43b24705e2 Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:38:17 -0400 Subject: [PATCH 3/6] Update build config --- .env.example | 2 -- Dockerfile | 2 -- docker-compose.yml | 4 +--- requirements.build.txt | 1 + 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index f139386..9606e56 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,6 @@ # Docker Image Settings IMAGE_NAME=uigf-api CONTAINER_NAME=UIGF-API -IMAGE_TAG=2.0 -EXTERNAL_PORT=3052 # App Settings TOKEN=AppToken diff --git a/Dockerfile b/Dockerfile index 1e57072..8aa6029 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,5 @@ # syntax=docker/dockerfile:1.6 - # Build stage FROM python:3.12 AS builder WORKDIR /code @@ -43,5 +42,4 @@ COPY --from=builder /code/dist/main /app/main # HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ # CMD curl -fsS http://localhost:8080/health || exit 1 -EXPOSE 8080 ENTRYPOINT ["/app/main"] diff --git a/docker-compose.yml b/docker-compose.yml index cafa8e2..6e25100 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,10 +6,8 @@ services: context: . dockerfile: Dockerfile target: runtime - image: ${IMAGE_NAME}:${IMAGE_TAG} + image: ${IMAGE_NAME}:latest container_name: ${CONTAINER_NAME}-Server - ports: - - "${EXTERNAL_PORT}:8080" volumes: - ./cache:/app/cache - ./dict:/app/dict diff --git a/requirements.build.txt b/requirements.build.txt index fb36633..2fa0832 100644 --- a/requirements.build.txt +++ b/requirements.build.txt @@ -1 +1,2 @@ pyinstaller==6.10.0 +setuptools<81 From 6ad7c69121223c0217899a4597087f0a29ea6605 Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:42:44 -0400 Subject: [PATCH 4/6] Update Dockerfile Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8aa6029..0ef577d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ && pip wheel -r requirements.txt -w /wheels \ && pip wheel -r requirements.build.txt -w /wheels -# Coyp source code +# Copy source code COPY . /code # Install dependencies from wheels cache (offline installation) From 20743a4f2cb699aa0e0af9aa39e17cebb4d65404 Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:42:59 -0400 Subject: [PATCH 5/6] Update requirements.build.txt Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- requirements.build.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.build.txt b/requirements.build.txt index 2fa0832..4be7835 100644 --- a/requirements.build.txt +++ b/requirements.build.txt @@ -1,2 +1,3 @@ pyinstaller==6.10.0 +# setuptools is pinned below version 81 due to incompatibility with PyInstaller; see https://github.com/pyinstaller/pyinstaller/issues/7994 setuptools<81 From b8f552a81d019470f6b49385bb4dac339f1d6b62 Mon Sep 17 00:00:00 2001 From: Masterain Date: Sun, 26 Oct 2025 20:43:20 -0400 Subject: [PATCH 6/6] Update Dockerfile Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0ef577d..1b9f8c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,8 +22,7 @@ RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ # Run PyInstaller to create a single executable RUN --mount=type=cache,target=/root/.cache/pip,id=pip-cache \ - pip install --no-index --find-links=/wheels pyinstaller \ - && pyinstaller -F main.py + pyinstaller -F main.py # Runtime stage