4.9 KiB · text History 3208487
# Builder and runner follow the sibling apps (cartosnap, gitgud): Debian
# rather than Alpine, to avoid DNS resolution issues in production.
#
#   - https://hub.docker.com/r/hexpm/elixir/tags
#   - https://hub.docker.com/_/debian?tab=tags&page=1&name=trixie
#
# OTP must be >= 27.3: older public_key rejects the certificate chain
# builds.hex.pm serves, and `mix local.hex` dies at image build. The same
# versions are pinned in .forgejo/workflows/checks.yml; move them together.
ARG ELIXIR_VERSION=1.20.4
ARG OTP_VERSION=28.5.0.8
ARG DEBIAN_VERSION=trixie-20261020-slim

ARG BUILDER_IMAGE="docker.io/hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="docker.io/debian:${DEBIAN_VERSION}"

FROM ${BUILDER_IMAGE} AS builder

# Build tools only:
#   * build-essential — bcrypt's C NIF, the linker for the Rust NIFs, and
#     the C compiler zstd-sys builds zstd's vendored sources with (statically,
#     for vigil_symbolic)
#   * git — the heroicons and daisyui dependencies are git checkouts, and
#     neiam_auth comes from the forge over HTTPS
#   * curl, ca-certificates — to bootstrap rustup
# No npm: assets/ has no package.json, and tailwind and esbuild are
# installed through mix (`mix assets.setup`).
RUN apt-get update -y \
  && apt-get install -y --no-install-recommends build-essential git curl ca-certificates \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

# Rust via rustup, pinned, for the NIF crates in native/ (vigil_codec,
# vigil_ua, vigil_sourcemap, vigil_symbolic), which Rustler builds from
# source during `mix compile`: nothing precompiled is downloaded. The
# crates' MSRV is 1.91; this is the toolchain gitgud builds with, and the
# one CI runs clippy and rustfmt with (.forgejo/workflows/checks.yml). The
# crates link nothing from the system (vigil_symbolic's zstd is compiled in
# statically), so neither image needs a system library for them.
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:${PATH} \
    RUST_VERSION=1.92.0
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
  | sh -s -- -y --profile minimal --default-toolchain ${RUST_VERSION} \
  && rustc --version && cargo --version

WORKDIR /app

RUN mix local.hex --force && \
    mix local.rebar --force

ENV MIX_ENV="prod"

COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

# Compile-time config first, so a config change recompiles the deps.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile

RUN mix assets.setup

COPY priv priv
COPY lib lib

# The crates' sources (each with its committed Cargo.lock): Rustler runs
# cargo in them from `mix compile`.
COPY native native

COPY assets assets

# Compile before bundling so LiveView's colocated hooks
# (`phoenix-colocated/vigil`, imported by js/app.js) exist on disk.
RUN mix compile
RUN mix assets.deploy

# Changes to config/runtime.exs don't require recompiling the code.
COPY config/runtime.exs config/

# bin/server, bin/migrate, bin/invite, bin/invite-link, bin/token, and
# env.sh.eex (the node name for clustering).
COPY rel rel

RUN mix release

FROM ${RUNNER_IMAGE}

# The Erlang runtime's needs and nothing else (the NIFs link only libc and
# libgcc_s, which the base image has):
#   * libstdc++6, openssl, libncurses6, locales, ca-certificates — the BEAM,
#     TLS to Postgres, S3, SMTP, webhooks and uptime checks
#   * tini — PID 1: forwards signals so connections close cleanly on
#     SIGTERM, and reaps zombies
# `upgrade` first: Debian's security fixes since the base tag was cut.
RUN apt-get update -y \
  && apt-get upgrade -y --no-install-recommends \
  && apt-get install -y --no-install-recommends \
       libstdc++6 openssl libncurses6 locales ca-certificates tini \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG=en_US.UTF-8 \
    LANGUAGE=en_US:en \
    LC_ALL=en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# Uploaded files (sourcemaps, artifact bundles, debug files) when no
# S3_BUCKET is set: Vigil.Storage.Local under VIGIL_STORAGE_ROOT. Mount a
# volume here, shared by every replica, or set S3_* (docs/deploy.md).
ENV VIGIL_STORAGE_ROOT=/data/storage
RUN mkdir -p /data/storage && chown -R nobody /data
VOLUME ["/data/storage"]

ENV MIX_ENV="prod" \
    PORT=4000

COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/vigil ./

USER nobody

EXPOSE 4000

# /_health/ (liveness: the node is up) over plain HTTP with bash's
# /dev/tcp, so the image needs no curl. Host: localhost, which prod's
# force_ssl leaves alone (config/prod.exs).
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
  CMD ["bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/${PORT:-4000} && printf 'GET /_health/ HTTP/1.0\\r\\nHost: localhost\\r\\n\\r\\n' >&3 && head -n 1 <&3 | grep -q ' 200 '"]

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/app/bin/server"]