shortstats/Dockerfile
2026-10-31 23:25:50 +01:00

89 lines
3.8 KiB
Docker

# syntax=docker/dockerfile:1
#
# Un seul Dockerfile, deux images :
# docker build --target collector -t shortstats-collector .
# docker build --target web -t shortstats-web .
#
# Les deux images partagent la même base Python et le même paquet ;
# chacune n'installe que les dépendances dont elle a besoin.
ARG PYTHON_IMAGE=python:3.13-slim-trixie
ARG NODE_IMAGE=node:24-trixie-slim
# ---------------------------------------------------------------------------
# 1. Bibliothèques de diagrammes (Vega) regroupées en un seul fichier local
FROM ${NODE_IMAGE} AS vendor
WORKDIR /build
COPY package.json package-lock.json ./
RUN npm ci --no-audit --no-fund
COPY frontend/ frontend/
RUN npm run build
# ---------------------------------------------------------------------------
# 2. Construction du paquet Python (wheel)
FROM ${PYTHON_IMAGE} AS build
ENV PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /src
COPY pyproject.toml ./
COPY src/ src/
COPY --from=vendor /build/src/shortstats/web/static/vendor/ src/shortstats/web/static/vendor/
RUN pip wheel --no-deps --wheel-dir /wheels .
# ---------------------------------------------------------------------------
# 3. Base d'exécution commune : utilisateur non privilégié, pas de shell de connexion
FROM ${PYTHON_IMAGE} AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
RUN useradd --system --uid 10001 --user-group --home-dir /app --no-create-home --shell /usr/sbin/nologin shortstats \
&& mkdir -p /app && chown shortstats:shortstats /app
WORKDIR /app
COPY constraints.txt /tmp/constraints.txt
# La CI ajoute org.opencontainers.image.source (adresse réelle du dépôt) :
# Forgejo s'en sert pour rattacher l'image au dépôt dans l'onglet « Paquets ».
LABEL org.opencontainers.image.title="shortstats" \
org.opencontainers.image.licenses="UNLICENSED"
# ---------------------------------------------------------------------------
# 4. Image du collecteur : supercronic exécute `shortstats collect` selon COLLECT_CRON
FROM runtime AS collector
ARG TARGETARCH
ARG SUPERCRONIC_VERSION=v0.2.51
ARG SUPERCRONIC_SHA1_AMD64=1692066e350a88ff4d7d6474fe3c7a6c41ec5e89
ARG SUPERCRONIC_SHA1_ARM64=9d4c2000493215a1898502b2fc2dada3e8ea8c7a
ADD https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-${TARGETARCH:-amd64} /usr/local/bin/supercronic
RUN set -eu; \
case "${TARGETARCH:-amd64}" in \
amd64) sum="${SUPERCRONIC_SHA1_AMD64}" ;; \
arm64) sum="${SUPERCRONIC_SHA1_ARM64}" ;; \
*) echo "Architecture non prévue : ${TARGETARCH}" >&2; exit 1 ;; \
esac; \
printf '%s /usr/local/bin/supercronic\n' "${sum}" > /tmp/supercronic.sha1; \
sha1sum -c /tmp/supercronic.sha1; \
rm /tmp/supercronic.sha1; \
chmod 0755 /usr/local/bin/supercronic
RUN --mount=type=bind,from=build,source=/wheels,target=/wheels \
pip install --constraint /tmp/constraints.txt "$(ls /wheels/shortstats-*.whl)[collector]"
COPY --chmod=0755 docker/collector-entrypoint.sh /usr/local/bin/collector-entrypoint
USER 10001:10001
ENV COLLECT_CRON="*/15 * * * *" \
RUN_ON_START=true
ENTRYPOINT ["collector-entrypoint"]
CMD ["cron"]
# ---------------------------------------------------------------------------
# 5. Image du site web
FROM runtime AS web
RUN --mount=type=bind,from=build,source=/wheels,target=/wheels \
pip install --constraint /tmp/constraints.txt "$(ls /wheels/shortstats-*.whl)[web]"
USER 10001:10001
ENV WEB_HOST=0.0.0.0 \
WEB_PORT=8000 \
USERS_FILE=/config/users.txt \
FORWARDED_ALLOW_IPS=127.0.0.1
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD ["python", "-c", "import os, urllib.request; urllib.request.urlopen(f'http://127.0.0.1:{os.environ.get(\"WEB_PORT\", \"8000\")}/healthz', timeout=4)"]
ENTRYPOINT ["shortstats"]
CMD ["web"]