# Copyright (c) 2024-2026 ERDesigns (Ernst Reidinga). All rights reserved.
#
# The runtime image of the self-hosted reporting container: one static musl binary, the shipped
# font set, and nothing else. Built from a staged context (scripts/local-release/report-server.sh):
#
#   erd-report-serve        the binary (fully static, from the renderer's own build)
#   fonts/                  fonts/index.json and the 130 families (62 MB)
#   resources/              an empty host-resources directory templates may name
#
# `FROM scratch` is the point: no libc, no shell, no package manager, nothing to patch. That is
# why HEALTHCHECK uses the exec form and the binary has `--health` — there is no shell to run a
# command in. The CA bundle is copied from a builder stage only so a PDF time-stamp request can
# verify an RFC 3161 authority over TLS, which is the one outbound connection the engine makes.
#
# Base image pinned by digest like the other release images (the multi-arch index; amd64 release).
FROM node:26-bookworm@sha256:a546d3f8acd2af4382668a7e6acd3bc4418e6a27478373e3246f8162f5181509 AS certs

FROM scratch
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY erd-report-serve /usr/local/bin/erd-report-serve
COPY fonts/ /usr/share/erd-report/fonts/
COPY resources/ /usr/share/erd-report/resources/

# The listener and the host directories the server reads at start (see
# docs/REPORT_SERVER.md). A licence is supplied at run time; none is baked in.
ENV ERD_REPORT_LISTEN=0.0.0.0 \
    ERD_REPORT_PORT=8080 \
    ERD_REPORT_FONTS_DIR=/usr/share/erd-report/fonts \
    ERD_REPORT_RESOURCES_DIR=/usr/share/erd-report/resources

# A numeric user, because a scratch image has no /etc/passwd to name one, and nothing in the
# image is written at run time (no request, template or document ever touches disk).
USER 65532:65532
EXPOSE 8080
STOPSIGNAL SIGTERM
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD ["/usr/local/bin/erd-report-serve", "--health"]
ENTRYPOINT ["/usr/local/bin/erd-report-serve"]
