# syntax=docker/dockerfile:1
# mcp-gateway-guard: container image (linux/amd64 and linux/arm64).
#
#   docker build -t mcp-gateway-guard .
#   docker run -i --rm -v "$PWD/config:/app/config:ro" -v "$PWD/logs:/app/logs" mcp-gateway-guard
#   docker run --rm mcp-gateway-guard --check --json
#
# What is installed is exactly uv.lock (hash-checked), without the dev tools:
# the runtime dependencies plus the `otel` extra, so OpenTelemetry export can
# be switched on in gateway.yaml. Base images are pinned by digest (Dependabot
# keeps them current). The embedding model is downloaded at build time, so the
# container never reaches the network at runtime. It runs as a non-root user.
#
# Mount your own config/ (gateway.yaml + policies.yaml) and a writable logs/
# for the audit log. The image names the config with MCP_GATEWAY_CONFIG.
# stdio downstream servers run *inside* this container, so their commands must
# exist in the image (the example servers do).
#
# Health: in Streamable HTTP mode, GET /readyz and /livez (see docs/operations.md).
# There is no HEALTHCHECK here because the default (stdio) mode has no port.

FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv

FROM python:3.13-slim-trixie@sha256:e48f59cdba69a3cbd9ed2cf0f348d90d45a8d350d98134cf1070bc106ee9ed90 AS build
COPY --from=uv /uv /usr/local/bin/uv
ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=never \
    UV_PYTHON=/usr/local/bin/python3 \
    UV_PROJECT_ENVIRONMENT=/opt/venv \
    UV_NO_CACHE=1
WORKDIR /src
# Dependencies first (a layer that survives source changes), then the project.
COPY pyproject.toml uv.lock README.md ./
RUN uv sync --locked --no-install-project --no-editable --extra otel
COPY src ./src
RUN uv sync --locked --no-editable --extra otel \
 && /opt/venv/bin/mcp-gateway-guard --version
# Bake the model into the image (and nothing else from the network at runtime).
RUN FASTEMBED_CACHE_PATH=/opt/models /opt/venv/bin/python -c \
    "from pathlib import Path; from mcp_gateway.index import FastEmbedEmbedder; \
FastEmbedEmbedder('BAAI/bge-base-en-v1.5', Path('/opt/models'), allow_download=True)"

FROM python:3.13-slim-trixie@sha256:e48f59cdba69a3cbd9ed2cf0f348d90d45a8d350d98134cf1070bc106ee9ed90
ARG VERSION=dev
ARG REVISION=unknown
LABEL org.opencontainers.image.title="mcp-gateway-guard" \
      org.opencontainers.image.description="Local MCP security gateway: tool search, default-deny policy firewall, approvals, audit log" \
      org.opencontainers.image.source="https://github.com/vinayak533/mcp-gateway" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.version="${VERSION}" \
      org.opencontainers.image.revision="${REVISION}"
ENV PYTHONUNBUFFERED=1 \
    PATH=/opt/venv/bin:$PATH \
    FASTEMBED_CACHE_PATH=/opt/models \
    HF_HUB_OFFLINE=1 \
    MCP_GATEWAY_CONFIG=/app/config/gateway.yaml
RUN useradd --create-home --uid 10001 --shell /usr/sbin/nologin gateway
COPY --from=build /opt/venv /opt/venv
COPY --from=build --chown=root:root /opt/models /opt/models
WORKDIR /app
COPY --chown=root:root config ./config
COPY --chown=root:root examples ./examples
RUN mkdir -p logs .cache/examples && chown gateway:gateway logs .cache/examples
USER gateway
STOPSIGNAL SIGTERM
ENTRYPOINT ["mcp-gateway-guard"]
