From 0d4c743403fbbdae684cf57930595a6140a0c232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sun, 15 Sep 2024 22:45:00 +0200 Subject: [PATCH] Ship using container image (#8) * Ignore files when creating container image * Add Containerfile Generated using mix phx.gen.release --docker. Updated to use Alpine instead of Debian (-50% disk usage), and stripped away comments. --- .containerignore | 16 ++++++++++++++++ Containerfile | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 .containerignore create mode 100644 Containerfile diff --git a/.containerignore b/.containerignore new file mode 100644 index 0000000..bf1359b --- /dev/null +++ b/.containerignore @@ -0,0 +1,16 @@ +.dockerignore +.git +!.git/HEAD +!.git/refs +/cover/ +/doc/ +/test/ +/tmp/ +.elixir_ls +/_build/ +/deps/ +*.ez +erl_crash.dump +/assets/node_modules/ +/priv/static/assets/ +/priv/static/cache_manifest.json diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..a91d738 --- /dev/null +++ b/Containerfile @@ -0,0 +1,50 @@ +ARG ELIXIR_VERSION=1.17.2 +ARG OTP_VERSION=27.0.1 +ARG ALPINE_VERSION=3.20.3 + +ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-alpine-${ALPINE_VERSION}" +ARG RUNNER_IMAGE="alpine:${ALPINE_VERSION}" + +FROM ${BUILDER_IMAGE} as builder + +RUN apk add --no-cache build-base git python3 curl + +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 + +COPY config/config.exs config/${MIX_ENV}.exs config/ +RUN mix deps.compile + +COPY priv priv +COPY lib lib +COPY assets assets + +RUN mix assets.deploy +RUN mix compile + +COPY config/runtime.exs config/ + +COPY rel rel +RUN mix release + +FROM ${RUNNER_IMAGE} + +RUN apk add --no-cache libstdc++ openssl ncurses-libs + +WORKDIR "/app" +RUN chown nobody /app + +ENV MIX_ENV="prod" + +COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/mse25 ./ + +USER nobody + +CMD ["/app/bin/server"]