Skip to content

Commit

Permalink
feat: improve image builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Feb 7, 2024
1 parent 823fe3c commit 58a10e8
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 555 deletions.
10 changes: 6 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
/doc/
/test/
/tmp/
.elixir_ls
.elixir_ls/
.elixir-tools/
.lexical/

# Mix artifacts
/_build/
Expand All @@ -19,9 +21,9 @@
erl_crash.dump

# Static artifacts - These should be fetched and built inside the Docker image
/apps/plataforma_digital/assets/node_modules/
/apps/plataforma_digital/priv/static/assets/
/apps/plataforma_digital/priv/static/cache_manifest.json
/assets/node_modules/
/priv/static/assets/
/priv/static/cache_manifest.json

# Nix artifacts
/.postgres/
Expand Down
82 changes: 82 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
ARG ELIXIR_VERSION=1.15.4
ARG OTP_VERSION=25.3.2.5
ARG DEBIAN_VERSION=buster-20230612

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

FROM ${BUILDER_IMAGE} as builder

# prepare build dir
WORKDIR /app

RUN apt-get update -y
RUN apt-get install -y build-essential curl
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs
RUN apt-get clean && rm -f /var/lib/apt/lists/*_*

# 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 ./
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

# compile assets
COPY assets assets
# RUN echo $(npm --version) && exit 1
RUN npm ci --prefix assets
RUN mix assets.deploy

COPY lib lib

# 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
RUN apt-get install -y libstdc++6 openssl libncurses5 locales
RUN 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/pescarte ./

USER nobody

CMD ["/app/bin/server"]
13 changes: 4 additions & 9 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ VERSION 0.7

deps:
ARG ELIXIR=1.15.4
ARG OTP=26.0.2
FROM hexpm/elixir:${ELIXIR}-erlang-${OTP}-debian-buster-20230612-slim
ARG OTP=25.3.2.5
ARG DEBIAN_VERSION=buster-20230612
FROM hexpm/elixir:${ELIXIR}-erlang-${OTP}-debian-${DEBIAN_VERSION}
RUN apt update -y && apt install -y build-essential
WORKDIR /src
COPY mix.exs mix.lock ./
Expand Down Expand Up @@ -59,13 +60,7 @@ release:
SAVE ARTIFACT /src/_build/prod/rel/pescarte /app/_build/prod/rel/pescarte AS LOCAL release

docker-prod:
FROM debian:buster-20230612-slim
RUN apt update -y && apt install -y openssl libncurses5
WORKDIR /app
RUN chown nobody /app
USER nobody
COPY +release/app/_build/prod/rel/pescarte .
CMD ["./bin/pescarte", "eval", "Pescarte.Release.migrate", "&&", "exec", "./bin/pescarte", "start"]
FROM DOCKERFILE .
ARG GITHUB_REPO
SAVE IMAGE --push ghcr.io/$GITHUB_REPO:prod

Expand Down
Loading

0 comments on commit 58a10e8

Please sign in to comment.