Skip to content

Commit e0966b6

Browse files
committed
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
1 parent 7b8223d commit e0966b6

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

Dockerfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1
2+
13
# Frontend build stage
24
FROM oven/bun:1 AS frontend-builder
35

@@ -7,7 +9,8 @@ WORKDIR /app
79
COPY lightrag_webui/ ./lightrag_webui/
810

911
# Build frontend assets for inclusion in the API package
10-
RUN cd lightrag_webui \
12+
RUN --mount=type=cache,target=/root/.bun/install/cache \
13+
cd lightrag_webui \
1114
&& bun install --frozen-lockfile \
1215
&& bun run build
1316

@@ -40,7 +43,8 @@ COPY setup.py .
4043
COPY uv.lock .
4144

4245
# Install base, API, and offline extras without the project to improve caching
43-
RUN uv sync --frozen --no-dev --extra api --extra offline --no-install-project --no-editable
46+
RUN --mount=type=cache,target=/root/.local/share/uv \
47+
uv sync --frozen --no-dev --extra api --extra offline --no-install-project --no-editable
4448

4549
# Copy project sources after dependency layer
4650
COPY lightrag/ ./lightrag/
@@ -49,7 +53,8 @@ COPY lightrag/ ./lightrag/
4953
COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui
5054

5155
# Sync project in non-editable mode and ensure pip is available for runtime installs
52-
RUN uv sync --frozen --no-dev --extra api --extra offline --no-editable \
56+
RUN --mount=type=cache,target=/root/.local/share/uv \
57+
uv sync --frozen --no-dev --extra api --extra offline --no-editable \
5358
&& /app/.venv/bin/python -m ensurepip --upgrade
5459

5560
# Prepare offline cache directory and pre-populate tiktoken data
@@ -81,7 +86,8 @@ ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH
8186

8287
# Install dependencies with uv sync (uses locked versions from uv.lock)
8388
# And ensure pip is available for runtime installs
84-
RUN uv sync --frozen --no-dev --extra api --extra offline --no-editable \
89+
RUN --mount=type=cache,target=/root/.local/share/uv \
90+
uv sync --frozen --no-dev --extra api --extra offline --no-editable \
8591
&& /app/.venv/bin/python -m ensurepip --upgrade
8692

8793
# Create persistent data directories AFTER package installation

Dockerfile.lite

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1
2+
13
# Frontend build stage
24
FROM oven/bun:1 AS frontend-builder
35

@@ -7,7 +9,8 @@ WORKDIR /app
79
COPY lightrag_webui/ ./lightrag_webui/
810

911
# Build frontend assets for inclusion in the API package
10-
RUN cd lightrag_webui \
12+
RUN --mount=type=cache,target=/root/.bun/install/cache \
13+
cd lightrag_webui \
1114
&& bun install --frozen-lockfile \
1215
&& bun run build
1316

@@ -40,7 +43,8 @@ COPY setup.py .
4043
COPY uv.lock .
4144

4245
# Install project dependencies (base + API extras) without the project to improve caching
43-
RUN uv sync --frozen --no-dev --extra api --no-install-project --no-editable
46+
RUN --mount=type=cache,target=/root/.local/share/uv \
47+
uv sync --frozen --no-dev --extra api --no-install-project --no-editable
4448

4549
# Copy project sources after dependency layer
4650
COPY lightrag/ ./lightrag/
@@ -49,7 +53,8 @@ COPY lightrag/ ./lightrag/
4953
COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui
5054

5155
# Sync project in non-editable mode and ensure pip is available for runtime installs
52-
RUN uv sync --frozen --no-dev --extra api --no-editable \
56+
RUN --mount=type=cache,target=/root/.local/share/uv \
57+
uv sync --frozen --no-dev --extra api --no-editable \
5358
&& /app/.venv/bin/python -m ensurepip --upgrade
5459

5560
# Prepare tiktoken cache directory and pre-populate tokenizer data
@@ -81,7 +86,8 @@ ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH
8186

8287
# Sync dependencies inside the final image using uv
8388
# And ensure pip is available for runtime installs
84-
RUN uv sync --frozen --no-dev --extra api --no-editable \
89+
RUN --mount=type=cache,target=/root/.local/share/uv \
90+
uv sync --frozen --no-dev --extra api --no-editable \
8591
&& /app/.venv/bin/python -m ensurepip --upgrade
8692

8793
# Create persistent data directories

docs/DockerDeployment.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,19 @@ LightRAG can be configured using environment variables in the `.env` file:
5959

6060
Docker instructions work the same on all platforms with Docker Desktop installed.
6161

62+
### Build Optimization
63+
64+
The Dockerfile uses BuildKit cache mounts to significantly improve build performance:
65+
66+
- **Automatic cache management**: BuildKit is automatically enabled via `# syntax=docker/dockerfile:1` directive
67+
- **Faster rebuilds**: Only downloads changed dependencies when `uv.lock` or `bun.lock` files are modified
68+
- **Efficient package caching**: UV and Bun package downloads are cached across builds
69+
- **No manual configuration needed**: Works out of the box in Docker Compose and GitHub Actions
70+
6271
### Start LightRAG server:
6372

6473
```bash
65-
docker-compose up -d
74+
docker compose up -d
6675
```
6776

6877
LightRAG Server uses the following paths for data storage:
@@ -77,9 +86,9 @@ data/
7786

7887
To update the Docker container:
7988
```bash
80-
docker-compose pull
81-
docker-compose down
82-
docker-compose up
89+
docker compose pull
90+
docker compose down
91+
docker compose up
8392
```
8493

8594
### Offline deployment
@@ -91,10 +100,15 @@ Software packages requiring `transformers`, `torch`, or `cuda` will is not prein
91100
### For local development and testing
92101

93102
```bash
94-
# Build and run with docker-compose
103+
# Build and run with Docker Compose (BuildKit automatically enabled)
95104
docker compose up --build
105+
106+
# Or explicitly enable BuildKit if needed
107+
DOCKER_BUILDKIT=1 docker compose up --build
96108
```
97109

110+
**Note**: BuildKit is automatically enabled by the `# syntax=docker/dockerfile:1` directive in the Dockerfile, ensuring optimal caching performance.
111+
98112
### For production release
99113

100114
**multi-architecture build and push**:

0 commit comments

Comments
 (0)