From ef6ed429eda4284c5471e5a8494a059fd1803f8a Mon Sep 17 00:00:00 2001 From: yangdx Date: Thu, 16 Oct 2025 13:16:22 +0800 Subject: [PATCH] Optimize Docker builds with layer caching and add pip for runtime installs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Split deps and source code layers • Add --no-editable flag to uv sync • Install pip for runtime packages • Improve build cache efficiency --- Dockerfile | 13 ++++++++++--- Dockerfile.offline | 15 +++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6cf36822..7a61270a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,13 +38,19 @@ RUN mkdir -p /root/.local/share/uv COPY pyproject.toml . COPY setup.py . COPY uv.lock . + +# Install project dependencies (base + API extras) without the project to improve caching +RUN uv sync --frozen --no-dev --extra api --no-install-project --no-editable + +# Copy project sources after dependency layer COPY lightrag/ ./lightrag/ # Include pre-built frontend assets from the previous stage COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui -# Install project dependencies (base + API extras) -RUN uv sync --frozen --no-dev --extra api +# Sync project in non-editable mode and ensure pip is available for runtime installs +RUN uv sync --frozen --no-dev --extra api --no-editable \ + && /app/.venv/bin/python -m ensurepip --upgrade # Final stage FROM python:3.12-slim @@ -68,7 +74,8 @@ COPY uv.lock . ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH # Sync dependencies inside the final image using uv -RUN uv sync --frozen --no-dev --extra api +RUN uv sync --frozen --no-dev --extra api --no-editable \ + && /app/.venv/bin/python -m ensurepip --upgrade # Create persistent data directories RUN mkdir -p /app/data/rag_storage /app/data/inputs diff --git a/Dockerfile.offline b/Dockerfile.offline index be6744b2..67aa92b6 100644 --- a/Dockerfile.offline +++ b/Dockerfile.offline @@ -38,13 +38,19 @@ RUN mkdir -p /root/.local/share/uv COPY pyproject.toml . COPY setup.py . COPY uv.lock . + +# Install base, API, and offline extras without the project to improve caching +RUN uv sync --frozen --no-dev --extra api --extra offline --no-install-project --no-editable + +# Copy project sources after dependency layer COPY lightrag/ ./lightrag/ # Include pre-built frontend assets from the previous stage COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui -# Install base, API, and offline extras so CLI helpers work during build -RUN uv sync --frozen --no-dev --extra api --extra offline +# Sync project in non-editable mode and ensure pip is available for runtime installs +RUN uv sync --frozen --no-dev --extra api --extra offline --no-editable \ + && /app/.venv/bin/python -m ensurepip --upgrade # Prepare offline cache directory and pre-populate tiktoken data # Use uv run to execute commands from the virtual environment @@ -74,8 +80,9 @@ COPY uv.lock . ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH # Install dependencies with uv sync (uses locked versions from uv.lock) -# IMPORTANT: Must be done BEFORE creating data/ directory to avoid setuptools error -RUN uv sync --frozen --no-dev --extra api --extra offline +# And ensure pip is available for runtime installs +RUN uv sync --frozen --no-dev --extra api --extra offline --no-editable \ + && /app/.venv/bin/python -m ensurepip --upgrade # Create persistent data directories AFTER package installation RUN mkdir -p /app/data/rag_storage /app/data/inputs /app/data/tiktoken