Shrink Dockerfile #10

Manually merged
nathanwoodburn merged 2 commits from feat/shrink_docker into main 2026-03-21 15:52:40 +11:00
3 changed files with 40 additions and 37 deletions

View File

@@ -4,6 +4,7 @@
FROM python:3.13-alpine AS build FROM python:3.13-alpine AS build
# Install build dependencies for Pillow and other native wheels # Install build dependencies for Pillow and other native wheels
# Kept in case source builds are needed, though wheels are preferred
RUN apk add --no-cache \ RUN apk add --no-cache \
build-base \ build-base \
jpeg-dev zlib-dev freetype-dev jpeg-dev zlib-dev freetype-dev
@@ -12,52 +13,47 @@ RUN apk add --no-cache \
COPY --from=ghcr.io/astral-sh/uv:0.8.21 /uv /uvx /bin/ COPY --from=ghcr.io/astral-sh/uv:0.8.21 /uv /uvx /bin/
WORKDIR /app WORKDIR /app
# Copy dependency files
COPY pyproject.toml uv.lock ./ COPY pyproject.toml uv.lock ./
# Install dependencies into a virtual environment # Install dependencies into a virtual environment
# - --frozen: strict lockfile usage
# - --no-dev: exclude development dependencies
# - --no-install-project: avoid installing app as package
# - --compile-bytecode: ensuring .pyc files for startup speed (optional, omit if size is critical but usually worth it)
# We omit --compile-bytecode here to save space as requested
RUN --mount=type=cache,target=/root/.cache/uv \ RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
# Copy only app source files uv sync --frozen --no-dev --no-install-workspace
COPY blueprints blueprints
COPY main.py server.py curl.py tools.py mail.py cache_helper.py ascii_art.py ./
COPY templates templates
COPY data data
COPY pwa pwa
COPY .well-known .well-known
# Clean up caches and pycache
RUN rm -rf /root/.cache/uv
RUN find . -type d -name "__pycache__" -exec rm -rf {} +
### Runtime stage ### ### Runtime stage ###
FROM python:3.13-alpine AS runtime FROM python:3.13-alpine AS runtime
ENV PATH="/app/.venv/bin:$PATH" ENV PATH="/app/.venv/bin:$PATH"
# Create non-root user # Create non-root user and install curl for healthchecks
RUN addgroup -g 1001 appgroup && \ RUN addgroup -g 1001 appgroup && \
adduser -D -u 1001 -G appgroup -h /app appuser adduser -D -u 1001 -G appgroup -h /app appuser && \
apk add --no-cache curl
WORKDIR /app WORKDIR /app
RUN apk add --no-cache curl
# Copy the virtual environment from build stage
# Copy only whats needed for runtime
COPY --from=build --chown=appuser:appgroup /app/.venv /app/.venv COPY --from=build --chown=appuser:appgroup /app/.venv /app/.venv
COPY --from=build --chown=appuser:appgroup /app/blueprints /app/blueprints
COPY --from=build --chown=appuser:appgroup /app/templates /app/templates # Copy all top-level Python files
COPY --from=build --chown=appuser:appgroup /app/data /app/data COPY --chown=appuser:appgroup *.py ./
COPY --from=build --chown=appuser:appgroup /app/pwa /app/pwa
COPY --from=build --chown=appuser:appgroup /app/.well-known /app/.well-known # Copy application directories
COPY --from=build --chown=appuser:appgroup /app/main.py /app/ COPY --chown=appuser:appgroup blueprints blueprints
COPY --from=build --chown=appuser:appgroup /app/server.py /app/ COPY --chown=appuser:appgroup templates templates
COPY --from=build --chown=appuser:appgroup /app/curl.py /app/ COPY --chown=appuser:appgroup data data
COPY --from=build --chown=appuser:appgroup /app/tools.py /app/ COPY --chown=appuser:appgroup pwa pwa
COPY --from=build --chown=appuser:appgroup /app/mail.py /app/ COPY --chown=appuser:appgroup .well-known .well-known
COPY --from=build --chown=appuser:appgroup /app/cache_helper.py /app/
COPY --from=build --chown=appuser:appgroup /app/ascii_art.py /app/
USER appuser USER appuser
EXPOSE 5000 EXPOSE 5000
ENTRYPOINT ["python3", "main.py"] ENTRYPOINT ["python3", "main.py"]

View File

@@ -19,6 +19,7 @@ from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_H
from ansi2html import Ansi2HTMLConverter from ansi2html import Ansi2HTMLConverter
from PIL import Image from PIL import Image
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import argparse
# Import blueprints # Import blueprints
from blueprints import now, blog, wellknown, api, podcast, acme, spotify from blueprints import now, blog, wellknown, api, podcast, acme, spotify
@@ -678,4 +679,10 @@ def not_found(e):
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, port=5000, host="127.0.0.1") # If --host argument is passed, use that as host, otherwise use 127.0.0.1
parser = argparse.ArgumentParser(description="Run the Flask server.")
parser.add_argument("--host", type=str, default="127.0.0.1")
args = parser.parse_args()
app.run(debug=True, port=5000, host=args.host)

View File

@@ -10,15 +10,15 @@ HTTP 200
GET http://127.0.0.1:5000/now/24_02_18 GET http://127.0.0.1:5000/now/24_02_18
HTTP 200 HTTP 200
GET http://127.0.0.1:5000/now/now.json GET http://127.0.0.1:5000/now.json
HTTP 200 HTTP 200
GET http://127.0.0.1:5000/now/now.xml GET http://127.0.0.1:5000/now.xml
HTTP 200 HTTP 200
GET http://127.0.0.1:5000/now/now.rss GET http://127.0.0.1:5000/now.rss
HTTP 200 HTTP 200
GET http://127.0.0.1:5000/now/rss.xml GET http://127.0.0.1:5000/rss.xml
HTTP 200 HTTP 200