Add BuildKit cache mounts to optimize Docker build performance

- Enable BuildKit syntax directive
- Cache UV and Bun package downloads
- Update docs for cache optimization
- Improve rebuild efficiency
This commit is contained in:
yangdx
2025-11-03 12:40:30 +08:00
parent 7b8223daad
commit e0966b6511
3 changed files with 39 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
# syntax=docker/dockerfile:1
# Frontend build stage # Frontend build stage
FROM oven/bun:1 AS frontend-builder FROM oven/bun:1 AS frontend-builder
@@ -7,7 +9,8 @@ WORKDIR /app
COPY lightrag_webui/ ./lightrag_webui/ COPY lightrag_webui/ ./lightrag_webui/
# Build frontend assets for inclusion in the API package # Build frontend assets for inclusion in the API package
RUN cd lightrag_webui \ RUN --mount=type=cache,target=/root/.bun/install/cache \
cd lightrag_webui \
&& bun install --frozen-lockfile \ && bun install --frozen-lockfile \
&& bun run build && bun run build
@@ -40,7 +43,8 @@ COPY setup.py .
COPY uv.lock . COPY uv.lock .
# Install base, API, and offline extras without the project to improve caching # 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 RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --extra offline --no-install-project --no-editable
# Copy project sources after dependency layer # Copy project sources after dependency layer
COPY lightrag/ ./lightrag/ COPY lightrag/ ./lightrag/
@@ -49,7 +53,8 @@ COPY lightrag/ ./lightrag/
COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui
# Sync project in non-editable mode and ensure pip is available for runtime installs # 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 \ RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --extra offline --no-editable \
&& /app/.venv/bin/python -m ensurepip --upgrade && /app/.venv/bin/python -m ensurepip --upgrade
# Prepare offline cache directory and pre-populate tiktoken data # Prepare offline cache directory and pre-populate tiktoken data
@@ -81,7 +86,8 @@ ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH
# Install dependencies with uv sync (uses locked versions from uv.lock) # Install dependencies with uv sync (uses locked versions from uv.lock)
# And ensure pip is available for runtime installs # And ensure pip is available for runtime installs
RUN uv sync --frozen --no-dev --extra api --extra offline --no-editable \ RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --extra offline --no-editable \
&& /app/.venv/bin/python -m ensurepip --upgrade && /app/.venv/bin/python -m ensurepip --upgrade
# Create persistent data directories AFTER package installation # Create persistent data directories AFTER package installation

View File

@@ -1,3 +1,5 @@
# syntax=docker/dockerfile:1
# Frontend build stage # Frontend build stage
FROM oven/bun:1 AS frontend-builder FROM oven/bun:1 AS frontend-builder
@@ -7,7 +9,8 @@ WORKDIR /app
COPY lightrag_webui/ ./lightrag_webui/ COPY lightrag_webui/ ./lightrag_webui/
# Build frontend assets for inclusion in the API package # Build frontend assets for inclusion in the API package
RUN cd lightrag_webui \ RUN --mount=type=cache,target=/root/.bun/install/cache \
cd lightrag_webui \
&& bun install --frozen-lockfile \ && bun install --frozen-lockfile \
&& bun run build && bun run build
@@ -40,7 +43,8 @@ COPY setup.py .
COPY uv.lock . COPY uv.lock .
# Install project dependencies (base + API extras) without the project to improve caching # 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 RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --no-install-project --no-editable
# Copy project sources after dependency layer # Copy project sources after dependency layer
COPY lightrag/ ./lightrag/ COPY lightrag/ ./lightrag/
@@ -49,7 +53,8 @@ COPY lightrag/ ./lightrag/
COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui
# Sync project in non-editable mode and ensure pip is available for runtime installs # Sync project in non-editable mode and ensure pip is available for runtime installs
RUN uv sync --frozen --no-dev --extra api --no-editable \ RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --no-editable \
&& /app/.venv/bin/python -m ensurepip --upgrade && /app/.venv/bin/python -m ensurepip --upgrade
# Prepare tiktoken cache directory and pre-populate tokenizer data # Prepare tiktoken cache directory and pre-populate tokenizer data
@@ -81,7 +86,8 @@ ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH
# Sync dependencies inside the final image using uv # Sync dependencies inside the final image using uv
# And ensure pip is available for runtime installs # And ensure pip is available for runtime installs
RUN uv sync --frozen --no-dev --extra api --no-editable \ RUN --mount=type=cache,target=/root/.local/share/uv \
uv sync --frozen --no-dev --extra api --no-editable \
&& /app/.venv/bin/python -m ensurepip --upgrade && /app/.venv/bin/python -m ensurepip --upgrade
# Create persistent data directories # Create persistent data directories

View File

@@ -59,10 +59,19 @@ LightRAG can be configured using environment variables in the `.env` file:
Docker instructions work the same on all platforms with Docker Desktop installed. Docker instructions work the same on all platforms with Docker Desktop installed.
### Build Optimization
The Dockerfile uses BuildKit cache mounts to significantly improve build performance:
- **Automatic cache management**: BuildKit is automatically enabled via `# syntax=docker/dockerfile:1` directive
- **Faster rebuilds**: Only downloads changed dependencies when `uv.lock` or `bun.lock` files are modified
- **Efficient package caching**: UV and Bun package downloads are cached across builds
- **No manual configuration needed**: Works out of the box in Docker Compose and GitHub Actions
### Start LightRAG server: ### Start LightRAG server:
```bash ```bash
docker-compose up -d docker compose up -d
``` ```
LightRAG Server uses the following paths for data storage: LightRAG Server uses the following paths for data storage:
@@ -77,9 +86,9 @@ data/
To update the Docker container: To update the Docker container:
```bash ```bash
docker-compose pull docker compose pull
docker-compose down docker compose down
docker-compose up docker compose up
``` ```
### Offline deployment ### Offline deployment
@@ -91,10 +100,15 @@ Software packages requiring `transformers`, `torch`, or `cuda` will is not prein
### For local development and testing ### For local development and testing
```bash ```bash
# Build and run with docker-compose # Build and run with Docker Compose (BuildKit automatically enabled)
docker compose up --build docker compose up --build
# Or explicitly enable BuildKit if needed
DOCKER_BUILDKIT=1 docker compose up --build
``` ```
**Note**: BuildKit is automatically enabled by the `# syntax=docker/dockerfile:1` directive in the Dockerfile, ensuring optimal caching performance.
### For production release ### For production release
**multi-architecture build and push**: **multi-architecture build and push**: