# syntax=docker/dockerfile:1
# VerityRF container images on Debian 12 (bookworm-slim, glibc 2.36), for linux/amd64,
# linux/arm64 and linux/arm/v7. Two targets share one base holding both binaries:
#   modem     (the default target) runs the headless modem `verityrf`
#   observer  runs the observer `verityrf-observer` and its web page (ARG OBSERVER_PORT)
#
# The binaries are built beforehand (Ubuntu 22.04 containers, glibc 2.35) and come in through the
# named build context `bin`, one directory per architecture: amd64/, arm64/, armv7/, each holding
# verityrf and verityrf-observer. tools/docker-image.sh builds them and this image:
#
#   tools/docker-image.sh                           every architecture, loaded as verityrf:<version>-<arch>
#   docker buildx build --build-context bin=DIR --platform linux/arm64 -t verityrf docker
#   docker buildx build --build-context bin=DIR --target observer -t verityrf-observer docker
#
# The image runs as the non-root user `verityrf` (UID/GID 1000, groups audio, dialout and
# plugdev); compose files and `docker run --user` choose another UID/GID. tini is PID 1 and
# passes SIGTERM (docker stop) to the program, which (the modem) unkeys the transmitter and
# exits. Every config key of the modem and every option of the observer is also read from an
# environment variable (docs/env-vars.md, docs/observer-env-vars.md).

# ---- the binaries of the target architecture, checked
FROM --platform=$BUILDPLATFORM debian:bookworm-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc AS binaries
ARG TARGETARCH
ARG TARGETVARIANT
COPY --from=bin / /bin-all/
RUN set -eu; \
    d=/bin-all/$TARGETARCH$TARGETVARIANT; \
    for f in verityrf verityrf-observer; do \
      if [ ! -f "$d/$f" ]; then \
        echo "no $f for $TARGETARCH$TARGETVARIANT in the bin context (tools/docker-image.sh builds it)" >&2; \
        exit 1; \
      fi; \
      install -D -m 0755 "$d/$f" "/out/$f"; \
    done

# ---- the runtime base: both binaries, the entrypoints, the user
FROM debian:bookworm-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc AS base
ARG VERSION=0.0.0
# Debian's archive as it was at this time (snapshot.debian.org), with the base image's digest above:
# the same packages in every build of the image (README "Reproducible builds")
ARG DEBIAN_SNAPSHOT=20261104T000000Z
LABEL org.opencontainers.image.version="$VERSION" \
      org.opencontainers.image.licenses="GPL-3.0-or-later"

# pulseaudio-utils: pactl, through which `--list-devices` lists PulseAudio devices
RUN set -eu; \
    sed -i -e "s|http://deb.debian.org/debian-security|http://snapshot.debian.org/archive/debian-security/$DEBIAN_SNAPSHOT|" \
      -e "s|http://deb.debian.org/debian|http://snapshot.debian.org/archive/debian/$DEBIAN_SNAPSHOT|" \
      /etc/apt/sources.list.d/debian.sources; \
    apt-get -o Acquire::Check-Valid-Until=false update; \
    apt-get install -y --no-install-recommends libpulse0 libasound2 pulseaudio-utils tini; \
    rm -rf /var/lib/apt/lists/* /var/cache/debconf/*-old /var/log/apt/* /var/log/dpkg.log; \
    groupadd -g 1000 verityrf; \
    useradd -u 1000 -g verityrf -G audio,dialout,plugdev -M -d /var/lib/verityrf -s /usr/sbin/nologin verityrf; \
    mkdir -p /config /logs /var/lib/verityrf /run/pulse; \
    chmod 1777 /logs /var/lib/verityrf

COPY --from=binaries /out/ /usr/local/bin/
COPY --chmod=0755 entrypoint.sh /usr/local/bin/verityrf-entrypoint
COPY --chmod=0755 observer-entrypoint.sh /usr/local/bin/verityrf-observer-entrypoint
COPY --chmod=0755 healthcheck.sh /usr/local/bin/verityrf-health

# HOME is writable by any UID (libpulse keeps its client state there)
ENV HOME=/var/lib/verityrf \
    PULSE_SERVER=unix:/run/pulse/native

USER verityrf
WORKDIR /var/lib/verityrf
STOPSIGNAL SIGTERM

# ---- the observer: verityrf-observer and its web page
FROM base AS observer
# the web page's port: the one place it is set (the page's address, EXPOSE, the healthcheck)
ARG OBSERVER_PORT=8321
LABEL org.opencontainers.image.title="VerityRF observer" \
      org.opencontainers.image.description="VerityRF observer: listens to VARA HF sessions and serves what they carry as text, JSON and a web page"
# the observer's defaults, as its option variables (docker/README.md): monitor and
# transcript on, the web page on every interface; verityrf-observer-entrypoint adds
# --device default when no input is named
ENV VERITYRF_OBSERVER_MONITOR=true \
    VERITYRF_OBSERVER_TRANSCRIPT=true \
    VERITYRF_OBSERVER_HTTP=0.0.0.0:${OBSERVER_PORT} \
    VERITYRF_HEALTH_PORT=${OBSERVER_PORT}
EXPOSE ${OBSERVER_PORT}
# the web page answering GET / (VERITYRF_HEALTH_PORT=off: always healthy, for runs without it)
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 CMD ["verityrf-health"]
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/verityrf-observer-entrypoint"]

# ---- the modem (the default target)
FROM base AS modem
LABEL org.opencontainers.image.title="VerityRF" \
      org.opencontainers.image.description="VerityRF headless modem (VARA HF / VARA FM host interface) and observer"
# read by verityrf-entrypoint (docker/README.md)
ENV VERITYRF_CONFIG=/config/verityrf.toml \
    VERITYRF_BIND=0.0.0.0 \
    VERITYRF_STATUS_PORT=8320 \
    VERITYRF_HOST_STATUS_BIND=0.0.0.0 \
    VERITYRF_LOG_DIR=/logs \
    VERITYRF_HEALTH_PORT=8320
EXPOSE 8300 8301 8100 8320 8321
# the status port's web interface answering GET / (the page needs no token)
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 CMD ["verityrf-health"]
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/verityrf-entrypoint"]
