Skip to content

Instantly share code, notes, and snippets.

@hectorperez
Last active January 11, 2023 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hectorperez/e0a3e2bd7b81d2f609a3d0b492d9e415 to your computer and use it in GitHub Desktop.
Save hectorperez/e0a3e2bd7b81d2f609a3d0b492d9e415 to your computer and use it in GitHub Desktop.
ARG ELIXIR_VERSION=1.13.4
ARG OTP_VERSION=25.0.3
ARG DEBIAN_VERSION=bullseye-20210902-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
# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# prepare build dir
WORKDIR /app
# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force
# set build ENV
ENV MIX_ENV="prod"
# install mix dependencies
COPY mix.exs mix.lock ./
ENV NOTESCLUB_IS_OBAN_WEB_PRO_ENABLED="true"
RUN --mount=type=secret,id=OBAN_KEY_FINGERPRINT \
--mount=type=secret,id=OBAN_LICENSE_KEY \
mix hex.repo add oban https://getoban.pro/repo \
--fetch-public-key "$(cat /run/secrets/OBAN_KEY_FINGERPRINT)" \
--auth-key "$(cat /run/secrets/OBAN_LICENSE_KEY)"
RUN mix deps.get --only $MIX_ENV
RUN mkdir config
# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile
COPY priv priv
COPY lib lib
COPY assets assets
# compile assets
RUN mix assets.deploy
# Compile the release
RUN mix compile
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/
COPY rel rel
RUN mix release
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}
RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
WORKDIR "/app"
RUN chown nobody /app
# set runner ENV
ENV MIX_ENV="prod"
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/notesclub ./
USER nobody
CMD ["sh", "-c", "/app/bin/notesclub eval Notesclub.Release.migrate && exec /app/bin/server"]
@hectorperez
Copy link
Author

The latest Dockerfile is on https://github.com/notesclub/notesclub/blob/main/Dockerfile

We install tailwind and esbuild in that step to take advantage of caching:

RUN mix do deps.compile, tailwind.install --if-missing, esbuild.install --if-missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment