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

# -----------------------------------
# Base Image #1: Elixir Builder
# - This is used for building later
#   docker image, with a development
#   toolset.
# -----------------------------------
FROM elixir:<%= elixir_version %> AS elixir-builder

# If you're using a hex mirror:
# ARG HEX_MIRROR=https://my_hex_mirror

RUN mix local.hex --force
RUN mix local.rebar --force

RUN mkdir -p /root/.config/rebar3 && \
  echo '{plugins, [rebar3_hex]}.' > /root/.config/rebar3/rebar.config

RUN /root/.mix/rebar3 plugins upgrade rebar3_hex

# -----------------------------------
# Base Image #2: Elixir Runner
# - Elixir Application Runner
#   This is used as a simple operating
#   system image to host your
#   application
# -----------------------------------
FROM debian:buster as elixir-runner

# You can add any libraries required by your application
# here:

RUN apt-get update && \
  apt-get install -y \
  # If you're using `:crypto`, you'll need openssl installed \
  libssl-dev \
  locales

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

# -----------------------------------
# - stage: install
# - job: dependencies
# -----------------------------------
FROM elixir-builder AS deps

ARG MIX_ENV=prod

# If you're using a hex mirror:
# ARG HEX_MIRROR=https://my_hex_mirror

WORKDIR /src

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

# If inside an umbrella project, you also need to add all `mix.exs`
# e.g
# COPY apps/my_app_1/mix.exs /src/apps/my_app_1/mix.exs
# COPY apps/my_app_2/mix.exs /src/apps/my_app_2/mix.exs

# If you're using your own organization on hex.pm, uncomment the
# following lines:
# ARG HEX_AUTH_KEY
# RUN mix hex.organization auth my_org --key ${HEX_AUTH_KEY}

RUN mix deps.get --only $MIX_ENV

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

ARG MIX_ENV=prod
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/

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
# -----------------------------------
FROM elixir-runner AS release_image

ARG APP_REVISION=latest
ARG MIX_ENV=prod

USER nobody

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

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