Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

Refactor Dockerfile.backend into multi-stage build

Edwin Jose7b28d04fadd5
diff --git a/Dockerfile.backend b/Dockerfile.backend
index 9bcf966b7..0a019f8d4 100644
--- a/Dockerfile.backend
+++ b/Dockerfile.backend
@@ -1,56 +1,75 @@
-# Build arg: set INSTALL_DOCLING=1 to add in-process docling extra + model downloads. docling-serve is always installed; torch is not.
-ARG INSTALL_DOCLING=0
+# syntax=docker/dockerfile:1.4
+# BuildKit: enables cache mounts and better layer reuse.
+#
+# Build: docker build -t openrag-backend .
+# With in-process docling + models: docker build --build-arg INSTALL_DOCLING=1 -t openrag-backend .
# -----------------------------------------------------------------------------
-# Builder stage: install deps and optional docling models
+# Stage: base (system + uv)
# -----------------------------------------------------------------------------
-FROM python:3.13-slim AS builder
-
-ARG INSTALL_DOCLING=0
+FROM python:3.13-slim AS base
-RUN apt-get update && apt-get install -y \
+RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
+ ca-certificates \
openssl \
- git \
- build-essential \
+ \
+ && curl -LsSf https://astral.sh/uv/install.sh | sh \
&& rm -rf /var/lib/apt/lists/*
-RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:$PATH"
+# -----------------------------------------------------------------------------
+# Stage: builder (deps + app, optional docling models)
+# -----------------------------------------------------------------------------
+FROM base AS builder
+
+# Pass from build: --build-arg INSTALL_DOCLING=1 (default 0)
+ARG INSTALL_DOCLING=0
+
+# Build-time deps only (not in final image)
+RUN apt-get update && apt-get install -y --no-install-recommends \
+ git \
+ build-essential \
+ && rm -rf /var/lib/apt/lists/*
+
WORKDIR /app
+# Dependency layer: reuse as long as lockfile and pyproject don't change
COPY pyproject.toml uv.lock ./
+RUN --mount=type=cache,target=/root/.cache/uv,uid=0,gid=0 \
+ if [ "$INSTALL_DOCLING" = "1" ]; then uv sync --extra docling; else uv sync; fi
-# Default: docling-serve (required), no torch, no in-process docling extra. With INSTALL_DOCLING=1 add docling extra + models.
-RUN if [ "$INSTALL_DOCLING" = "1" ]; then uv sync --extra docling; else uv sync; fi
-
-# Conditional docling/EasyOCR model downloads (only when INSTALL_DOCLING=1). EasyOCR cache under /app for final stage.
-RUN if [ "$INSTALL_DOCLING" = "1" ]; then uv run docling-tools models download; fi
-RUN if [ "$INSTALL_DOCLING" = "1" ]; then uv run python -c "\
+# Optional: docling/EasyOCR models (single layer when INSTALL_DOCLING=1)
+RUN if [ "$INSTALL_DOCLING" = "1" ]; then \
+ uv run docling-tools models download \
+ && uv run python -c " \
import pathlib, easyocr; \
cache = pathlib.Path('/app/.EasyOCR/model'); \
cache.mkdir(parents=True, exist_ok=True); \
easyocr.Reader(['fr','de','es','en'], download_enabled=True, model_storage_directory=str(cache)); \
-print('EasyOCR cache ready at', cache)"; fi
-RUN if [ "$INSTALL_DOCLING" = "1" ]; then rm -rf /root/.cache/httpx /root/.cache/uv 2>/dev/null || true; fi
+print('EasyOCR cache ready at', cache) \
+" \
+ && rm -rf /root/.cache/httpx /root/.cache/uv 2>/dev/null || true; \
+ fi
+# Application (invalidates after deps only when src/flows change)
COPY src/ ./src/
COPY flows/ ./flows/
# -----------------------------------------------------------------------------
-# Final stage: minimal runtime image
+# Stage: runtime (minimal image)
# -----------------------------------------------------------------------------
-FROM python:3.13-slim
+FROM python:3.13-slim AS runtime
-RUN apt-get update && apt-get install -y \
+RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
+ ca-certificates \
openssl \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
-# Copy installed app and venv from builder (same Python base image so venv is valid)
COPY --from=builder /app /app
ENV VIRTUAL_ENV=/app/.venv