# syntax=docker/dockerfile:1
# Lab7 messenger image. Runs a per-replica nginx mTLS terminator in front of the
# loopback-only API (see src/api-nginx.conf). Non-root, tmpfs-friendly.
FROM python:3.11.9-slim-bookworm

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    APP_HOST=127.0.0.1 \
    APP_PORT=8080

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends nginx-light ca-certificates \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt ./
RUN python -m pip install --no-cache-dir -r requirements.txt

COPY src ./src
COPY migrations ./migrations

RUN useradd --create-home --uid 10001 --shell /usr/sbin/nologin appuser \
    && mkdir -p /tmp/nginx \
    && chown -R appuser:appuser /tmp/nginx /app

USER appuser

# 8443 = mTLS terminator (LB -> API); 8080 is loopback only.
EXPOSE 8443

HEALTHCHECK --interval=5s --timeout=3s --start-period=25s --retries=6 \
    CMD ["python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).status == 200 else 1)"]

CMD ["sh", "/app/src/api-entrypoint.sh"]
