From 7a94f361b1808098eb258784aeed2267b941bbf6 Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:38:32 -0500 Subject: [PATCH 1/3] fix(pydiscordsh): updated the dockerfile to be a bit smaller --- apps/pydiscordsh/Dockerfile | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/apps/pydiscordsh/Dockerfile b/apps/pydiscordsh/Dockerfile index bd3eb458b..69019d759 100644 --- a/apps/pydiscordsh/Dockerfile +++ b/apps/pydiscordsh/Dockerfile @@ -1,7 +1,7 @@ # Stage 1: Builder FROM python:3.12-slim as builder -# Set environment variables for Poetry and Python +# Set environment variables for Python and Poetry ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ POETRY_VERSION=1.8.2 \ @@ -18,33 +18,27 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /app -# Copy and install dependencies +# Copy and install dependencies using Poetry COPY pyproject.toml poetry.lock ./ -RUN poetry install --no-root --only main +RUN poetry export -f requirements.txt --without-hashes --output requirements.txt \ + && poetry install --no-root --only main -# Stage 2: Final lightweight image +# Stage 2: Final lightweight image using pip FROM python:3.12-slim -# Set the same environment variables for consistency +# Set environment variables for Python ENV PYTHONUNBUFFERED=1 \ - PIP_NO_CACHE_DIR=1 \ - POETRY_VERSION=1.8.2 \ - POETRY_HOME="/opt/poetry" \ - POETRY_VIRTUALENVS_IN_PROJECT=true \ - POETRY_NO_INTERACTION=1 - -# Copy Poetry from the builder stage -COPY --from=builder /opt/poetry /opt/poetry -COPY --from=builder /usr/local/bin/poetry /usr/local/bin/poetry + PIP_NO_CACHE_DIR=1 WORKDIR /app -# Copy the installed dependencies and the rest of the app -COPY --from=builder /app /app -COPY . . -# Install the app without dev dependencies -RUN poetry install --no-dev +# Copy the exported requirements and use pip instead of Poetry +COPY --from=builder /app/requirements.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the application source code +COPY . . -# Expose the port and run the server +# Expose the port and run the server using Python directly EXPOSE 3000 -CMD ["poetry", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] +CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] From 9dc5ddac5d24a633390c888cee5994e3233a549d Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:46:55 -0500 Subject: [PATCH 2/3] fix(pydiscordsh): alpine test case --- apps/pydiscordsh/Dockerfile | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/apps/pydiscordsh/Dockerfile b/apps/pydiscordsh/Dockerfile index 69019d759..c395b39eb 100644 --- a/apps/pydiscordsh/Dockerfile +++ b/apps/pydiscordsh/Dockerfile @@ -1,7 +1,6 @@ # Stage 1: Builder FROM python:3.12-slim as builder -# Set environment variables for Python and Poetry ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ POETRY_VERSION=1.8.2 \ @@ -9,7 +8,6 @@ ENV PYTHONUNBUFFERED=1 \ POETRY_VIRTUALENVS_IN_PROJECT=true \ POETRY_NO_INTERACTION=1 -# Install system dependencies and Poetry RUN apt-get update && apt-get install -y --no-install-recommends \ curl git \ && curl -sSL https://install.python-poetry.org | python3 - \ @@ -18,27 +16,39 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /app -# Copy and install dependencies using Poetry -COPY pyproject.toml poetry.lock ./ +# Copy the entire source code, not just the lock files +COPY . . + +# Export dependencies and compile bytecode RUN poetry export -f requirements.txt --without-hashes --output requirements.txt \ - && poetry install --no-root --only main + && poetry install --no-root --only main \ + && python -m compileall -b . + +# Stage 2: Bytecode Extractor +FROM python:3.12-slim as bytecode-extractor -# Stage 2: Final lightweight image using pip -FROM python:3.12-slim +WORKDIR /app +# Copy the entire app including the compiled bytecode +COPY --from=builder /app /app + +# Stage 3: Final Lightweight Image +FROM python:3.12-alpine -# Set environment variables for Python ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 WORKDIR /app -# Copy the exported requirements and use pip instead of Poetry -COPY --from=builder /app/requirements.txt requirements.txt +# Copy everything from the bytecode-extractor stage +COPY --from=bytecode-extractor /app/requirements.txt requirements.txt +COPY --from=bytecode-extractor /app . + +# Install dependencies using pip RUN pip install --no-cache-dir -r requirements.txt -# Copy the application source code -COPY . . +# Confirm the presence of the `main.py` file +RUN ls -la /app/main.py -# Expose the port and run the server using Python directly +# Expose the port and run the server using the ASGI app EXPOSE 3000 CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] From e862276cc10dfd415a161a6cee3ebc4c2e19ec8f Mon Sep 17 00:00:00 2001 From: h0lybyte <5599058+h0lybyte@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:44:18 -0500 Subject: [PATCH 3/3] fix(pydiscordsh): down to 100mb for now --- apps/pydiscordsh/Dockerfile | 63 ++++++++++++++++++++------------- apps/pydiscordsh/poetry.lock | 4 +-- apps/pydiscordsh/pyproject.toml | 2 +- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/apps/pydiscordsh/Dockerfile b/apps/pydiscordsh/Dockerfile index c395b39eb..3673d5dc0 100644 --- a/apps/pydiscordsh/Dockerfile +++ b/apps/pydiscordsh/Dockerfile @@ -1,6 +1,7 @@ # Stage 1: Builder -FROM python:3.12-slim as builder +FROM python:3.12-slim AS stage1 +# Set environment variables for Python and Poetry ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ POETRY_VERSION=1.8.2 \ @@ -8,6 +9,7 @@ ENV PYTHONUNBUFFERED=1 \ POETRY_VIRTUALENVS_IN_PROJECT=true \ POETRY_NO_INTERACTION=1 +# Install system dependencies and Poetry RUN apt-get update && apt-get install -y --no-install-recommends \ curl git \ && curl -sSL https://install.python-poetry.org | python3 - \ @@ -16,39 +18,50 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /app -# Copy the entire source code, not just the lock files -COPY . . - -# Export dependencies and compile bytecode +# Copy and install dependencies using Poetry +COPY pyproject.toml poetry.lock ./ RUN poetry export -f requirements.txt --without-hashes --output requirements.txt \ - && poetry install --no-root --only main \ - && python -m compileall -b . - -# Stage 2: Bytecode Extractor -FROM python:3.12-slim as bytecode-extractor - -WORKDIR /app -# Copy the entire app including the compiled bytecode -COPY --from=builder /app /app + && poetry install --no-root --only main -# Stage 3: Final Lightweight Image -FROM python:3.12-alpine +# Stage 2: Final lightweight image using pip +FROM python:3.12-slim AS stage2 +# Set environment variables for Python ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 WORKDIR /app -# Copy everything from the bytecode-extractor stage -COPY --from=bytecode-extractor /app/requirements.txt requirements.txt -COPY --from=bytecode-extractor /app . - -# Install dependencies using pip +# Copy the exported requirements and use pip instead of Poetry +COPY --from=stage1 /app/requirements.txt requirements.txt RUN pip install --no-cache-dir -r requirements.txt -# Confirm the presence of the `main.py` file -RUN ls -la /app/main.py +# Copy the application source code +COPY ./pydiscordsh pydiscordsh +COPY ./main.py main.py + +# Expose the port and run the server using Python directly +#EXPOSE 3000 +#CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] + +# Stage 3: Final production-ready Ubuntu base image +# FROM ubuntu/python:3.12-24.04_stable AS stage3 +FROM al3xos/python-distroless:3.12-debian12 AS stage3 + +# # Set environment variables +# ENV PYTHONUNBUFFERED=1 \ +# PIP_NO_CACHE_DIR=1 + +WORKDIR /app + +# # Copy the application and Python dependencies from stage2 +ENV PYTHONPATH=/usr/local/lib/python3.12/site-packages +COPY --from=stage2 /app/pydiscordsh pydiscordsh +COPY --from=stage2 /app/main.py main.py +COPY --from=stage2 /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages +COPY --from=stage2 /usr/local/bin /usr/local/bin + -# Expose the port and run the server using the ASGI app +# # Optional: Expose the port and run the server EXPOSE 3000 -CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] +ENTRYPOINT ["python3", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3000", "--ws-ping-interval", "25", "--ws-ping-timeout", "5"] \ No newline at end of file diff --git a/apps/pydiscordsh/poetry.lock b/apps/pydiscordsh/poetry.lock index 2796b0c88..901c88e47 100644 --- a/apps/pydiscordsh/poetry.lock +++ b/apps/pydiscordsh/poetry.lock @@ -1614,5 +1614,5 @@ propcache = ">=0.2.0" [metadata] lock-version = "2.0" -python-versions = ">=3.9,<3.13" -content-hash = "1aa5006dfbd14f4c2df32b9669fd55bd9f6ea955a1bd2cf869ba3acb65ab561c" +python-versions = ">=3.9,<3.14" +content-hash = "b4313e9fb0224d47be4114e5684c4fea9d6e9cc26a87a2908a8f96549cfb8afa" diff --git a/apps/pydiscordsh/pyproject.toml b/apps/pydiscordsh/pyproject.toml index 08c72fa8d..dafd7ad59 100644 --- a/apps/pydiscordsh/pyproject.toml +++ b/apps/pydiscordsh/pyproject.toml @@ -21,7 +21,7 @@ readme = 'README.md' include = "pydiscordsh" [tool.poetry.dependencies] - python = ">=3.9,<3.13" + python = ">=3.9,<3.14" fastapi = "^0.115.6" supabase = "^2.11.0" uvicorn = "^0.34.0"