# syntax=docker/dockerfile:1
# Containerfile for Librarian MCP Server
#
# Build:  podman build -t librarian .
# Run:    podman run -d -p 4242:4242 -v ~/Documents/notes:/app/data librarian

# ---------------------------------------------------------------------------
# Build stage: install dependencies and build the wheel
# ---------------------------------------------------------------------------
FROM python:3.12-slim AS builder

RUN pip install --no-cache-dir uv

WORKDIR /build

# Copy project metadata first for better layer caching
COPY pyproject.toml README.md ./
COPY src/ src/

# hatch-vcs needs git history for versioning; provide a fallback
# so builds from tarballs or shallow clones still work.
COPY .git/ .git/
RUN apt-get update && apt-get install -y --no-install-recommends git \
    && rm -rf /var/lib/apt/lists/*

# Install the package into a prefix we can copy to the runtime stage
RUN uv pip install --python /usr/local/bin/python --no-cache . \
    && uv pip install --python /usr/local/bin/python --no-cache uvicorn

# ---------------------------------------------------------------------------
# Runtime stage: minimal image with only what's needed to run
# ---------------------------------------------------------------------------
FROM python:3.12-slim

# Copy installed packages and entry-point scripts from builder
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin/librarian /usr/local/bin/librarian

# Entrypoint script
COPY container/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# In Podman rootless mode, container root maps to the host user,
# so running as root is safe and avoids volume permission issues.
ENV XDG_CONFIG_HOME=/root/.config

VOLUME /app/data
EXPOSE 4242

ENTRYPOINT ["entrypoint.sh"]
