FROM python:3.14-slim

WORKDIR /app

# jemalloc: glibc malloc never returns fragmented free pages to the OS, so
# bursty large-JSON workloads ratchet RSS up to the pod limit (lockstep
# OOMKills every ~3.4 days). jemalloc decays freed pages back to the OS.
# The ln+test makes the build fail loudly if the library path ever changes;
# a bad LD_PRELOAD path would otherwise be silently ignored at runtime.
RUN apt-get update && apt-get install -y --no-install-recommends libjemalloc2 \
    && rm -rf /var/lib/apt/lists/* \
    && ln -s "$(find /usr/lib -name 'libjemalloc.so.2' -print -quit)" /usr/local/lib/libjemalloc.so.2 \
    && test -f /usr/local/lib/libjemalloc.so.2
ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so.2

# Install uv for fast dependency management
COPY --from=ghcr.io/astral-sh/uv:0.11 /uv /usr/local/bin/uv

# Copy dependency files first for layer caching
COPY pyproject.toml uv.lock ./

# Install dependencies
RUN uv sync --frozen --no-dev

# Copy source code
COPY src/ src/

# HTTP transport: listen on all interfaces, port 8000
ENV CRUSTDATA_HTTP_HOST=0.0.0.0
ENV CRUSTDATA_HTTP_PORT=8000
ENV CRUSTDATA_HTTP_PATH=/mcp

EXPOSE 8000

CMD ["uv", "run", "crustdata-mcp-http"]
