2.7 KiB · text History 1ba5ec3
# Builder and runner follow the sibling apps (bigtrippr et al.): 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.
ARG ELIXIR_VERSION=1.20.4
ARG OTP_VERSION=28.5.0.8
ARG DEBIAN_VERSION=trixie-20261020-slim

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

FROM ${BUILDER_IMAGE} AS builder

# Build tools, plus Node for the npm-managed JS (Leaflet, markercluster,
# blurhash); tailwind and esbuild themselves are installed through mix.
RUN apt-get update -y \
    && apt-get install -y build-essential git curl ca-certificates gnupg \
    && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y nodejs \
    && apt-get clean && rm -f /var/lib/apt/lists/*_*

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

COPY priv priv
COPY lib lib

# JS deps before the rest of assets, so only a lockfile change busts this layer.
COPY assets/package.json assets/package-lock.json assets/
RUN cd assets && npm ci --no-audit --no-fund --omit=dev

COPY assets assets

# Compile before bundling so LiveView's colocated hooks
# (`phoenix-colocated/cartosnap_app`, 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 and bin/migrate.
COPY rel rel

RUN mix release

FROM ${RUNNER_IMAGE}

# imagemagick: uploads are read (blurhash) and resized (the ProcessImage
# worker) by shelling out to `magick`.
RUN apt-get update -y && \
  apt-get install -y libstdc++6 openssl libncurses6 locales ca-certificates tini imagemagick \
  && apt-get clean && rm -f /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

ENV MIX_ENV="prod"

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

USER nobody

# tini reaps zombies (ImageMagick's included) and forwards signals so
# database connections close cleanly on SIGTERM.
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/app/bin/server"]