FROM python:3.12.6-slim-bookworm

RUN groupadd --system --gid 10001 app \
 && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app

WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt

COPY --chown=app:app src/ /app/
COPY --chown=app:app migrations/ /app/migrations/

USER app

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    APP_HOST=0.0.0.0 \
    APP_PORT=8080 \
    APP_ENV=lab

EXPOSE 8080

ARG BUILD_ID=dev
LABEL lab.build-id=$BUILD_ID

STOPSIGNAL SIGTERM

HEALTHCHECK --interval=10s --timeout=3s --start-period=8s --retries=5 \
  CMD python3 -c "import urllib.request,sys; urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2); sys.exit(0)"

CMD ["python3", "/app/server.py"]
