Dockerfile 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # syntax=docker/dockerfile:1
  2. # Frontend build stage
  3. # Build frontend assets on the native build platform to avoid
  4. # cross-architecture emulation issues during multi-platform builds.
  5. FROM --platform=$BUILDPLATFORM oven/bun:1 AS frontend-builder
  6. WORKDIR /app
  7. # Copy frontend source code
  8. COPY lightrag_webui/ ./lightrag_webui/
  9. # Build frontend assets for inclusion in the API package
  10. RUN --mount=type=cache,target=/root/.bun/install/cache \
  11. cd lightrag_webui \
  12. && bun install --frozen-lockfile \
  13. && bun run build
  14. # Python build stage - using uv for faster package installation
  15. FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
  16. ENV DEBIAN_FRONTEND=noninteractive
  17. ENV UV_SYSTEM_PYTHON=1
  18. ENV UV_COMPILE_BYTECODE=1
  19. WORKDIR /app
  20. # Install system deps (Rust is required by some wheels)
  21. RUN apt-get update \
  22. && apt-get install -y --no-install-recommends \
  23. curl \
  24. build-essential \
  25. pkg-config \
  26. && rm -rf /var/lib/apt/lists/* \
  27. && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
  28. ENV PATH="/root/.cargo/bin:/root/.local/bin:${PATH}"
  29. # Ensure shared data directory exists for uv caches
  30. RUN mkdir -p /root/.local/share/uv
  31. # Copy project metadata and sources
  32. COPY pyproject.toml .
  33. COPY setup.py .
  34. COPY uv.lock .
  35. # Install base, API, and offline extras without the project to improve caching
  36. RUN --mount=type=cache,target=/root/.local/share/uv \
  37. uv sync --frozen --no-dev --extra api --extra offline --no-install-project --no-editable
  38. # Copy project sources after dependency layer
  39. COPY lightrag/ ./lightrag/
  40. # Include pre-built frontend assets from the previous stage
  41. COPY --from=frontend-builder /app/lightrag/api/webui ./lightrag/api/webui
  42. # Sync project in non-editable mode and ensure pip is available for runtime installs
  43. RUN --mount=type=cache,target=/root/.local/share/uv \
  44. uv sync --frozen --no-dev --extra api --extra offline --no-editable \
  45. && /app/.venv/bin/python -m ensurepip --upgrade
  46. # Prepare offline cache directory and pre-populate tiktoken data
  47. # Use uv run to execute commands from the virtual environment
  48. RUN mkdir -p /app/data/tiktoken \
  49. && uv run lightrag-download-cache --cache-dir /app/data/tiktoken || status=$?; \
  50. if [ -n "${status:-}" ] && [ "$status" -ne 0 ] && [ "$status" -ne 2 ]; then exit "$status"; fi
  51. # Final stage
  52. FROM python:3.12-slim
  53. WORKDIR /app
  54. # Install uv for package management
  55. COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
  56. ENV UV_SYSTEM_PYTHON=1
  57. # Copy installed packages and application code
  58. COPY --from=builder /root/.local /root/.local
  59. COPY --from=builder /app/.venv /app/.venv
  60. COPY --from=builder /app/lightrag ./lightrag
  61. COPY pyproject.toml .
  62. COPY setup.py .
  63. COPY uv.lock .
  64. # Ensure the installed scripts are on PATH
  65. ENV PATH=/app/.venv/bin:/root/.local/bin:$PATH
  66. # Install dependencies with uv sync (uses locked versions from uv.lock)
  67. # And ensure pip is available for runtime installs
  68. RUN --mount=type=cache,target=/root/.local/share/uv \
  69. uv sync --frozen --no-dev --extra api --extra offline --no-editable \
  70. && /app/.venv/bin/python -m ensurepip --upgrade
  71. # Create persistent data directories AFTER package installation
  72. RUN mkdir -p /app/data/rag_storage /app/data/inputs /app/data/prompts /app/data/tiktoken
  73. # Copy offline cache into the newly created directory
  74. COPY --from=builder /app/data/tiktoken /app/data/tiktoken
  75. # Point to the prepared cache
  76. ENV TIKTOKEN_CACHE_DIR=/app/data/tiktoken
  77. ENV WORKING_DIR=/app/data/rag_storage
  78. ENV INPUT_DIR=/app/data/inputs
  79. ENV PROMPT_DIR=/app/data/prompts
  80. # Expose API port
  81. EXPOSE 9621
  82. ENTRYPOINT ["python", "-m", "lightrag.api.lightrag_server"]