diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a03cecf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +.dockerignore +# there are valid reasons to keep the .git, namely so that you can get the +# current commit hash +#.git +.log +tmp + +# Mix artifacts +_build +deps +*.ez +releases + +# Generate on crash by the VM +erl_crash.dump + +# Static artifacts +node_modules diff --git a/Dockerfile b/Dockerfile index 087e661..cb66a94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,55 +1,76 @@ -FROM hexpm/elixir:1.13.3-erlang-23.2.3-alpine-3.15.0 as build +# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian instead of +# Alpine to avoid DNS resolution issues in production. +# +# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu +# https://hub.docker.com/_/ubuntu?tab=tags +# +# +# This file is based on these images: +# +# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image +# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20210902-slim - for the release image +# - https://pkgs.org/ - resource for finding needed packages +# - Ex: hexpm/elixir:1.13.3-erlang-23.2.3-debian-bullseye-20210902-slim +# +ARG DOCKER_IMAGE="hexpm/elixir:1.13.3-erlang-23.2.3-alpine-3.15.0" -WORKDIR /app + +FROM ${DOCKER_IMAGE} as builder + +# install build dependencies RUN apk add gcc git librdkafka-dev musl-dev -# install Hex + Rebar -RUN mix do local.hex --force, local.rebar --force +# prepare build dir +WORKDIR /app + +# install hex + rebar +RUN mix local.hex --force && \ + mix local.rebar --force # set build ENV -ENV MIX_ENV=prod +ENV MIX_ENV="prod" # install mix dependencies COPY mix.exs mix.lock ./ -COPY config config -RUN mix deps.get --only $MIX_ENV -RUN DEBUG=1 mix deps.compile -# -## build assets -#COPY assets assets -#RUN cd assets && npm install && npm run deploy -#RUN mix phx.digest -# build project -#COPY priv priv +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 + +# Compile the release COPY lib lib + RUN mix compile -# build release -# at this point we should copy the rel directory but -# we are not using it so we can omit it -# COPY rel rel +# Changes to config/runtime.exs don't require recompiling the code +COPY config/runtime.exs config/ + +COPY rel rel RUN mix release -# prepare release image -FROM hexpm/elixir:1.13.3-erlang-23.2.3-alpine-3.15.0 AS app +# start a new build stage so that the final image will only contain +# the compiled release and other runtime necessities +FROM ${DOCKER_IMAGE} -# install runtime dependencies -#RUN apk add --update bash openssl postgresql-client +# Set the locale +#RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen -EXPOSE 4000 -ENV MIX_ENV=prod +#ENV LANG en_US.UTF-8 +#ENV LANGUAGE en_US:en +#ENV LC_ALL en_US.UTF-8 -# prepare app directory -RUN mkdir /app -WORKDIR /app +WORKDIR "/app" +RUN chown nobody /app + +# Only copy the final release from the build stage +COPY --from=builder --chown=nobody:root /app/_build/prod/rel/kafkaex_lag_exporter ./ -# copy release to app container -COPY --from=build /app/_build/prod/rel/kafkaex_lag_exporter . -# COPY entrypoint.sh . -RUN chown -R nobody: /app USER nobody -ENV HOME=/app -CMD ["/app/bin/kafkaex_lag_exporter", "start"] +CMD ["/app/bin/server"] diff --git a/rel/overlays/bin/server b/rel/overlays/bin/server new file mode 100755 index 0000000..5237b88 --- /dev/null +++ b/rel/overlays/bin/server @@ -0,0 +1,3 @@ +#!/bin/sh +cd -P -- "$(dirname -- "$0")" +PHX_SERVER=true exec ./kafkaex_lag_exporter start