Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

docker: optimize build caching and eliminate expensive chown -R (#2132)

LearningCircuitd957675cf83e
diff --git a/.github/workflows/docker-tests.yml b/.github/workflows/docker-tests.yml
index 3ad3ca9750..599187699a 100644
--- a/.github/workflows/docker-tests.yml
+++ b/.github/workflows/docker-tests.yml
@@ -712,6 +712,126 @@ jobs:
${{ env.TEST_IMAGE }} \
sh -c "timeout 60s python examples/llm_integration/custom_research_example.py || true"
+ # Smoke test for the production ldr image
+ ldr-production-smoke-test:
+ runs-on: ubuntu-latest
+ name: Production Image Smoke Test
+ timeout-minutes: 30
+ permissions:
+ contents: read
+
+ steps:
+ - name: Harden the runner (Audit all outbound calls)
+ uses: step-security/harden-runner@5ef0c079ce82195b2a36a210272d6b661572d83e # v2.14.2
+ with:
+ egress-policy: audit
+
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ with:
+ persist-credentials: false
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3
+
+ - name: Build production ldr image
+ uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6
+ with:
+ context: .
+ target: ldr
+ load: true
+ tags: ldr-prod
+ # Cache poisoning protection: only read/write cache on trusted events (not PRs from forks)
+ cache-from: ${{ github.event_name != 'pull_request' && 'type=gha,scope=ldr-prod' || '' }}
+ cache-to: ${{ github.event_name != 'pull_request' && 'type=gha,mode=max,scope=ldr-prod' || '' }}
+
+ - name: Verify file ownership and playwright access
+ run: |
+ docker run --rm ldr-prod sh -c '
+ echo "Checking /install/.venv ownership..."
+ VENV_OWNER=$(stat -c "%U:%G" /install/.venv)
+ if [ "$VENV_OWNER" != "ldruser:ldruser" ]; then
+ echo "FAIL: /install/.venv owned by $VENV_OWNER, expected ldruser:ldruser"
+ exit 1
+ fi
+ echo "OK: /install/.venv owned by $VENV_OWNER"
+
+ echo "Spot-checking venv file ownership..."
+ BAD_FILES=$(find /install/.venv \( ! -user ldruser -o ! -group ldruser \) -print -quit)
+ if [ -n "$BAD_FILES" ]; then
+ echo "FAIL: files not owned by ldruser:ldruser:"
+ find /install/.venv \( ! -user ldruser -o ! -group ldruser \) | head -10
+ exit 1
+ fi
+ echo "OK: venv files owned by ldruser:ldruser"
+
+ echo "Checking /install parent dir ownership..."
+ INSTALL_OWNER=$(stat -c "%U:%G" /install)
+ if [ "$INSTALL_OWNER" != "ldruser:ldruser" ]; then
+ echo "FAIL: /install owned by $INSTALL_OWNER, expected ldruser:ldruser"
+ exit 1
+ fi
+ echo "OK: /install owned by $INSTALL_OWNER"
+
+ echo "Checking /scripts parent dir ownership..."
+ SCRIPTS_OWNER=$(stat -c "%U:%G" /scripts)
+ if [ "$SCRIPTS_OWNER" != "ldruser:ldruser" ]; then
+ echo "FAIL: /scripts owned by $SCRIPTS_OWNER, expected ldruser:ldruser"
+ exit 1
+ fi
+ echo "OK: /scripts owned by $SCRIPTS_OWNER"
+
+ echo "Checking /home/ldruser ownership..."
+ HOME_OWNER=$(stat -c "%U:%G" /home/ldruser)
+ if [ "$HOME_OWNER" != "ldruser:ldruser" ]; then
+ echo "FAIL: /home/ldruser owned by $HOME_OWNER, expected ldruser:ldruser"
+ exit 1
+ fi
+ echo "OK: /home/ldruser owned by $HOME_OWNER"
+
+ echo "Checking playwright is accessible as ldruser..."
+ gosu ldruser playwright --version
+ echo "OK: playwright accessible as ldruser"
+ '
+
+ - name: Start production container
+ run: |
+ mkdir -p "$PWD/ci-data-prod"
+ docker run -d \
+ --name ldr-prod-test \
+ -p 5001:5000 \
+ -v "$PWD/ci-data-prod":/data \
+ -e LDR_DATA_DIR=/data \
+ -e LDR_USE_FALLBACK_LLM=true \
+ -e DISABLE_RATE_LIMITING=true \
+ -e SECRET_KEY=test-secret-key-for-ci \
+ ldr-prod
+
+ - name: Wait for healthy status
+ run: |
+ echo "Waiting for server to be healthy..."
+ for i in {1..60}; do
+ if curl -sf http://localhost:5001/api/v1/health > /dev/null 2>&1; then
+ echo "Server is healthy after $i seconds"
+ exit 0
+ fi
+ if ! docker ps --filter "name=ldr-prod-test" --filter "status=running" -q | grep -q .; then
+ echo "Container died!"
+ docker logs ldr-prod-test
+ exit 1
+ fi
+ echo "Waiting... ($i/60)"
+ sleep 1
+ done
+ echo "Server failed to become healthy"
+ docker logs ldr-prod-test
+ exit 1
+
+ - name: Stop container
+ if: always()
+ run: |
+ docker stop ldr-prod-test || true
+ docker rm ldr-prod-test || true
+
# Run infrastructure tests (conditional on path changes)
infrastructure-tests:
runs-on: ubuntu-latest
diff --git a/Dockerfile b/Dockerfile
index d0ef52d49f..c50d657476 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,6 +6,8 @@ FROM python:3.14-slim@sha256:486b8092bfb12997e10d4920897213a06563449c951c5506c2a
# Set shell to bash with pipefail for safer pipe handling
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+ARG DEBIAN_FRONTEND=noninteractive
+
# Install system dependencies for SQLCipher and Node.js for frontend build
# Using Acquire::Retries to handle transient Debian mirror errors during CI
RUN apt-get update -o Acquire::Retries=3 && apt-get upgrade -y -o Acquire::Retries=3 \
@@ -58,16 +60,23 @@ ENV PDM_REQUEST_TIMEOUT=120
ARG DEPS_HASH
WORKDIR /install
+
+# Copy dependency files first (changes rarely)
COPY pyproject.toml pyproject.toml
COPY pdm.lock pdm.lock
-COPY src/ src
COPY LICENSE LICENSE
COPY README.md README.md
+
# Copy frontend build files
COPY package.json package.json
COPY package-lock.json* package-lock.json
COPY vite.config.js vite.config.js
+# Source files last (changes frequently) — keeps dependency layers cached
+# during rebuilds. Ordering within a stage does not affect the final filesystem;
+# the builder stage inherits all files from builder-base regardless of COPY order.
+COPY src/ src
+
####
# Builds the LDR service dependencies used in production.
####
@@ -190,6 +199,8 @@ FROM python:3.14-slim@sha256:486b8092bfb12997e10d4920897213a06563449c951c5506c2a
# Set shell to bash with pipefail for safer pipe handling
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+ARG DEBIAN_FRONTEND=noninteractive
+
# Upgrade pip to fix CVE-2026-1703 (malicious wheel extraction)
RUN pip3 install --no-cache-dir pip==26.0
@@ -220,15 +231,18 @@ RUN mkdir -p /app/.config/local_deep_research /home/ldruser/.local/share && \
chmod -R 755 /app /home/ldruser
# retrieve packages from build stage
-COPY --from=builder /install/.venv/ /install/.venv
+COPY --chown=ldruser:ldruser --from=builder /install/.venv/ /install/.venv
ENV PATH="/install/.venv/bin:$PATH"
-# Verify SQLCipher is available after copy using compatibility module
-# and install browser automation tools
-RUN python -c "from local_deep_research.database.sqlcipher_compat import get_sqlcipher_module; \
+# Verify SQLCipher, then install browsers — both as ldruser via gosu.
+# Running as ldruser ensures:
+# 1. Python __pycache__ files created during import are owned by ldruser
+# 2. Browser binaries land in ldruser's $HOME (~/.cache/ms-playwright/)
+# This avoids the need to chown 500MB+ of files later.
+RUN HOME=/home/ldruser gosu ldruser python -c "from local_deep_research.database.sqlcipher_compat import get_sqlcipher_module; \
sqlcipher = get_sqlcipher_module(); \
print(f'✓ SQLCipher module loaded successfully: {sqlcipher}')" \
- && playwright install
+ && HOME=/home/ldruser gosu ldruser playwright install
# Create volume for persistent configuration
# Use /app for configuration to support non-root user
@@ -237,15 +251,17 @@ VOLUME /app/.config/local_deep_research
# Create volume for Ollama start script
VOLUME /scripts/
# Copy the Ollama entrypoint script
-COPY scripts/ollama_entrypoint.sh /scripts/ollama_entrypoint.sh
+COPY --chown=ldruser:ldruser scripts/ollama_entrypoint.sh /scripts/ollama_entrypoint.sh
# Copy LDR entrypoint script to handle volume permissions
COPY scripts/ldr_entrypoint.sh /usr/local/bin/ldr_entrypoint.sh
-# Set permissions and ownership for scripts and directories
+# COPY --chown sets ownership on copied contents, but Docker auto-creates
+# parent dirs (/install, /scripts) as root. Fix with non-recursive chown
+# (fast — avoids walking 500MB+ of venv files that are already ldruser-owned).
RUN chmod +x /scripts/ollama_entrypoint.sh \
&& chmod +x /usr/local/bin/ldr_entrypoint.sh \
- && chown -R ldruser:ldruser /install /scripts /home/ldruser
+ && chown ldruser:ldruser /install /scripts
EXPOSE 5000