From d4731b7824136d3fe3c5218dc5af8ab6e58f19b4 Mon Sep 17 00:00:00 2001 From: Gaston Valvassori Date: Tue, 8 Jul 2025 14:24:54 -0300 Subject: [PATCH 1/3] chore: optimize dockerfile --- .dockerignore | 93 ++++++++++++++++++++++++++++++++++++++++++--------- Dockerfile | 13 ++++--- 2 files changed, 86 insertions(+), 20 deletions(-) diff --git a/.dockerignore b/.dockerignore index f1e12e7..9156db6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,21 +1,82 @@ -# repo and project folders -.devcontainer -.docs +# Version control .git -.github .gitignore -.gitmodules -.idea -.vscode - -# docker -Dockerfile -.dockerignore -docker-compose.yaml - -# python related -.pyright_cache -.pytest_cache +.gitattributes + +# Development containers +.devcontainer/ +.vscode/ + +# Documentation +*.md +docs/ +*.txt + +# Python cache and temporary files +__pycache__/ *.pyc *.pyo *.pyd +.Python +.pytest_cache/ +.coverage +.coverage.* +.cache +.mypy_cache/ +.ruff_cache/ +.dmypy.json +dmypy.json + +# Virtual environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Build artifacts +build/ +dist/ +*.egg-info/ +.eggs/ + +# IDE files +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS files +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Scripts (if not needed in container) +scripts/ + +# Log files +*.log +logs/ + +# Temporary files +tmp/ +temp/ +.tmp/ +.temp/ + +# Test artifacts +.tox/ +.nox/ +htmlcov/ +.coverage +coverage.xml +*.cover +*.py,cover +.hypothesis/ diff --git a/Dockerfile b/Dockerfile index 9474b39..ca6f3b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,11 +7,13 @@ ARG USER=appuser ENV RUNTIME_PACKAGES=libpq-dev # These packages will be deleted from the final image, after the application is packaged -ENV BUILD_PACKAGES=gcc +ENV BUILD_PACKAGES="gcc build-essential python3-dev" RUN apt-get update \ - && apt-get install -y ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \ - && apt-get clean && rm -rf /var/lib/apt/lists/* + && apt-get install -y --no-install-recommends ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* \ + && rm -rf /tmp/* /var/tmp/* RUN mkdir -p /opt/app/${PROJECT_NAME} @@ -32,10 +34,13 @@ RUN pip install --upgrade pip \ && pip install --user uv==${UV_VERSION} WORKDIR /opt/app/${PROJECT_NAME} -COPY --chown=${USER}:${USER} . . +# Instead of copying everything first, copy dependency files first +COPY --chown=${USER}:${USER} pyproject.toml uv.lock ./ RUN uv sync --frozen --no-cache --no-install-project --no-default-groups +# Then copy the rest of the application +COPY --chown=${USER}:${USER} . . # ---- # Devcontainer adds extra tools for development From df661af9617e77bdf84f8017f0d74bf2f8234f02 Mon Sep 17 00:00:00 2001 From: Gaston Valvassori Date: Thu, 10 Jul 2025 10:12:46 -0300 Subject: [PATCH 2/3] fix: deployment stage missing user and project args --- .dockerignore | 1 - Dockerfile | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 9156db6..72d8320 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,7 +8,6 @@ .vscode/ # Documentation -*.md docs/ *.txt diff --git a/Dockerfile b/Dockerfile index ca6f3b8..ac8b7ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,6 +46,7 @@ COPY --chown=${USER}:${USER} . . # Devcontainer adds extra tools for development FROM base AS devcontainer +ARG USER=appuser USER root # Add any other tool usefull during development to the following list, this won't be included @@ -85,6 +86,8 @@ RUN uv build --wheel # Deployment stage to run in cloud environments. This must be the last stage, which is used to run the application by default FROM base AS deployment +ARG PROJECT_NAME=python-template + # root is needed to remove build dependencies USER root RUN apt-get purge -y ${BUILD_PACKAGES} \ From 58bf14f3564eb209c5788de51038e45546a8e126 Mon Sep 17 00:00:00 2001 From: Gaston Valvassori Date: Thu, 10 Jul 2025 10:39:44 -0300 Subject: [PATCH 3/3] docs: improve docker layer docs --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ac8b7ad..937f763 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,7 +35,8 @@ RUN pip install --upgrade pip \ WORKDIR /opt/app/${PROJECT_NAME} -# Instead of copying everything first, copy dependency files first +# Only copy files needed to install dependencies so it can be cached +# (changes to source files won't invalidate cache at this point) COPY --chown=${USER}:${USER} pyproject.toml uv.lock ./ RUN uv sync --frozen --no-cache --no-install-project --no-default-groups