forked from mtusi2026/mtusi2026lab5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> broker: RabbitMQ 4.3.7 (тег+digest), quorum queue + DLX/DLQ из definitions, пользователи relay (только publish в exchange) и worker (только consume), без management UI и портов. relay: outbox -> publish с publisher confirm, reaper истёкших lease, страховка от потерянных сообщений. worker: claim + heartbeat lease, варианты, публикация с fencing, ack после COMMIT, graceful stop, уборка staging и мусора попыток. S3: два bucket и четыре identity (admin, api, worker, auditor) с проверкой модели доступа в s3-init. Отдельная сеть queue. Reconciler lab4 (трек прошлой лабы) удалён: его роль выполняет уборка по журналу попыток.
68 lines
2.9 KiB
Docker
68 lines
2.9 KiB
Docker
# syntax=docker/dockerfile:1
|
|
# Messenger, lab 5 (API replicas behind HAProxy, PostgreSQL + Valkey + S3 + RabbitMQ). Built by
|
|
# compose.yaml (services api, migrate, s3-init, relay, worker, auditor, jobctl: one image, different
|
|
# commands; only the worker imports Pillow).
|
|
#
|
|
# Python is interpreted, so there is no separate compile stage; the image
|
|
# contains only the interpreter, locked dependencies and the source code.
|
|
# Base image is pinned by version AND digest (python 3.13.16, Debian 12 bookworm).
|
|
FROM python:3.13.16-slim-bookworm@sha256:a1165e272e578941b84abc79e4ab38a0305cd12803a5c4247979ac7655f4d641
|
|
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PIP_NO_CACHE_DIR=1 \
|
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
|
|
|
WORKDIR /app
|
|
|
|
# 1. Dependencies first (layer cache survives source edits). Every package,
|
|
# including transitive ones, is pinned with sha256 hashes in requirements.txt;
|
|
# --no-deps guarantees nothing outside the lock file is installed.
|
|
COPY requirements.txt ./
|
|
RUN pip install --root-user-action=ignore --require-hashes --no-deps -r requirements.txt
|
|
|
|
# 2. JPEG integrity check: libjpeg-turbo's djpeg (strict mode, see src/messenger/images.py).
|
|
# Version pinned to the Debian 12 package; apt lists are removed in the same layer.
|
|
RUN apt-get update \
|
|
&& apt-get install -y --no-install-recommends libjpeg-turbo-progs=1:2.1.5-2 \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# 3. Unprivileged runtime user. Code stays owned by root, so the process
|
|
# cannot modify its own code.
|
|
RUN groupadd --system --gid 10001 app \
|
|
&& useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
|
|
|
COPY src/ ./src/
|
|
# Versioned SQL migrations, applied by `python -m messenger.migrate` (compose service migrate).
|
|
COPY migrations/ ./migrations/
|
|
# Course contract, served read-only at /openapi.json for Swagger UI (/docs).
|
|
COPY contracts/openapi.json ./contracts/openapi.json
|
|
|
|
# Non-secret defaults; compose.yaml passes the rest. Passwords come only as files
|
|
# (DATABASE_URL_FILE / CACHE_URL_FILE -> Docker secrets), never as ENV or COPY.
|
|
ENV PYTHONPATH=/app/src \
|
|
APP_HOST=0.0.0.0 \
|
|
APP_PORT=8080 \
|
|
APP_ENV=lab \
|
|
LOG_LEVEL=info \
|
|
SHUTDOWN_TIMEOUT_SECONDS=8 \
|
|
DOCS_ENABLED=true \
|
|
SESSION_TTL_SECONDS=3600 \
|
|
DATABASE_URL_FILE=/run/secrets/database_url
|
|
|
|
# Build revision (scripts/rolling-update.sh passes git commit + time): makes every rolled-out
|
|
# image distinguishable in `docker inspect` and in the "starting" log line of each replica.
|
|
ARG BUILD_REVISION=dev
|
|
LABEL org.opencontainers.image.revision=$BUILD_REVISION
|
|
ENV APP_REVISION=$BUILD_REVISION
|
|
|
|
USER app:app
|
|
EXPOSE 8080
|
|
STOPSIGNAL SIGTERM
|
|
|
|
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
|
CMD ["python", "-m", "messenger.healthcheck"]
|
|
|
|
# Exec form: the Python server is PID 1 and receives SIGTERM directly (no shell in between).
|
|
CMD ["python", "-m", "messenger"]
|