From 927c5a4681ba12702a72ae7ce9e628d43bc1697e Mon Sep 17 00:00:00 2001 From: CleverThis Date: Thu, 23 Apr 2026 19:04:57 +0000 Subject: [PATCH] chore(ci): optimize Dockerfile layer order to cache Python dependency installation Reorder COPY and RUN instructions in both Dockerfile and Dockerfile.server to leverage Docker's layer caching for the dependency installation step. Previously, pyproject.toml, README.md, and src/ were all copied together before running uv pip install, meaning any source code change would invalidate the dependency layer cache and force a full reinstall. The new pattern copies only the dependency manifests (pyproject.toml and uv.lock) first, installs the build tool in a cached layer, then copies README.md and src/ for the actual wheel build. This ensures the uv pip install step is only re-executed when pyproject.toml or uv.lock change, not on every source code change. ISSUES CLOSED: #1667 --- Dockerfile | 16 ++++++++++++---- Dockerfile.server | 14 ++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index eb6b1d466..ae5d61adb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,13 +12,21 @@ COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv # Set up working directory WORKDIR /app -# Copy project metadata and source code -COPY pyproject.toml README.md ./ +# Copy dependency manifests first to leverage Docker layer caching. +# The expensive dependency installation step is only re-executed when +# pyproject.toml or uv.lock change, not on every source code change. +COPY pyproject.toml uv.lock ./ + +# Install build tool — this layer is cached as long as pyproject.toml +# and uv.lock remain unchanged. +RUN uv pip install --system build + +# Copy remaining source files needed for the wheel build. +COPY README.md ./ COPY src/ ./src/ # Build wheel -RUN uv pip install --system build && \ - python -m build --wheel --outdir /dist +RUN python -m build --wheel --outdir /dist # Runtime stage FROM python:3.13-slim diff --git a/Dockerfile.server b/Dockerfile.server index a6d3ba482..65db5d24b 100644 --- a/Dockerfile.server +++ b/Dockerfile.server @@ -22,13 +22,19 @@ COPY --from=ghcr.io/astral-sh/uv:0.8.0 /uv /usr/local/bin/uv WORKDIR /app -# Copy project metadata and source code -COPY pyproject.toml README.md ./ -COPY src/ ./src/ +# Copy dependency manifests first to leverage Docker layer caching. +# The expensive dependency installation step is only re-executed when +# pyproject.toml or uv.lock change, not on every source code change. +COPY pyproject.toml uv.lock ./ -# Install build tool (separate layer for better caching) +# Install build tool (separate layer for better caching) — this layer is +# cached as long as pyproject.toml and uv.lock remain unchanged. RUN uv pip install --system build +# Copy remaining source files needed for the wheel build. +COPY README.md ./ +COPY src/ ./src/ + # Build wheel RUN python -m build --wheel --outdir /dist