# syntax = docker/dockerfile:1.0-experimental

# This Dockerfile was generated by [dockerize](https://hex.pm/packages/dockerize)
# at <%= NaiveDateTime.local_now |> to_string() %>
<%= if gen_command do %>
# with `<%= gen_command %>`
<% end %>

# It leverages multi-stage-building of docker to build as fast as possible.
# How stages work together: https://user-images.githubusercontent.com/43009/84713978-e59a2700-af9e-11ea-9bbd-9dcf28d23da7.png

# You are free to edit this dockerfile.

# -----------------------------------
# - stage: install
# - job: dependencies
# -----------------------------------

# see: https://github.com/qhwa/docker-elixir-builder
FROM qhwa/elixir-builder:<%= elixir_version %> AS deps

ARG MIX_ENV=prod
ARG HEX_MIRROR_URL=https://repo.hex.pm

WORKDIR /src

COPY config /src/config
COPY mix.exs mix.lock /src/

RUN mix deps.get --only $MIX_ENV

# -----------------------------------
# - stage: build
# - job: compile_deps
# -----------------------------------
FROM deps AS compile_deps
WORKDIR /src

ARG MIX_ENV=prod
ARG APPSIGNAL_HTTP_PROXY

RUN mix deps.compile


# -----------------------------------
# - stage: build
# - job: compile_app
# -----------------------------------
FROM compile_deps AS compile_app
WORKDIR /src

ARG MIX_ENV=prod

COPY lib/ ./lib

<%= if phoenix_assets do %>
COPY priv/ ./priv
<% else %>
# You may add other directories crucial for compiling, for example:
# COPY priv/ ./priv
<% end %>

RUN mix compile

<%= if phoenix_assets do %>

# -----------------------------------
# - stage: build
# - job: assets
# -----------------------------------
FROM deps AS assets

WORKDIR /src/assets

COPY assets/package.json assets/package-lock.json ./

ARG NPM_REGISTRY=https://registry.npmjs.com/
ARG APPSIGNAL_HTTP_PROXY

RUN npm \
  --registry ${NPM_REGISTRY} \
  --prefer-offline \
  --no-audit \
  --ignore-scripts \
  ci

ARG SASS_BINARY_SITE

RUN npm rebuild node-sass

COPY assets/ ./

RUN npm run deploy

# -----------------------------------
# - stage: build
# - job: digest
# -----------------------------------
FROM compile_deps AS digest
WORKDIR /src

ARG MIX_ENV=prod

COPY --from=assets /src/priv ./priv

RUN mix phx.digest
<% end %>

# -----------------------------------
# - stage: release
# - job: mix_release
# -----------------------------------
FROM compile_app AS mix_release

WORKDIR /src

ARG MIX_ENV=prod

<%= if phoenix_assets do %>
COPY --from=digest /src/priv/static ./priv/static
<% end %>

RUN mix release --path /app --quiet

# -----------------------------------
# - stage: release
# - job: release_image
# -----------------------------------

# See: https://github.com/qhwa/docker-elixir-runner
FROM qhwa/elixir-runner:<%= elixir_version %> AS release_image

ARG APP_REVISION=latest
ARG MIX_ENV=prod

RUN chown nobody:nobody /app

USER nobody

COPY --from=mix_release --chown=nobody:nobody /app /app

ENTRYPOINT ["/app/bin/<%= app %>"]
CMD ["start"]
