# syntax = docker/dockerfile:1.0-experimental

# This Dockerfile was generated by [dockerize](https://hex.pm/packages/dockerize)
# at <%= DateTime.utc_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 hexpm/elixir:<%= elixir_version %> AS elixir-builder

RUN apt update && apt install -y git build-essential

# Uncomment if you're using a hex mirror:
# ENV 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

ENV MIX_ENV=prod

# Uncomment if you're using a hex mirror:
# ENV 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

ENV MIX_ENV=prod
RUN mix deps.compile

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

ENV 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 %>
# ## -- BEGIN assets building with Node.js, NPM and webpack
# # -----------------------------------
# # - stage: build with NPM
# # - job: assets
# # - uncomment if you're using Nodejs,
# #   NPM and webpack
# # -----------------------------------
# FROM node:10-buster-slim as assets
# WORKDIR /src/assets
# ENV NODE_ENV=prod
# ENV NPM_REGISTRY=https://registry.npmjs.com/
# COPY assets/package.json assets/package-lock.json ./
# COPY --from=deps /src/deps/phoenix ../deps/phoenix
# COPY --from=deps /src/deps/phoenix_html ../deps/phoenix_html
# RUN npm \
#   --registry ${NPM_REGISTRY} \
#   --prefer-offline \
#   --no-audit \
#   ci
# COPY assets/ ./
# RUN npm run deploy
# 
# # -----------------------------------
# # - stage: build
# # - job: digest
# # -----------------------------------
# FROM compile_deps AS digest
# WORKDIR /src
# ENV MIX_ENV=prod
# COPY --from=assets /src/priv ./priv
# RUN mix phx.digest
# ## -- END building assets with Node.js, NPM, and Webpack

# -----------------------------------
# - stage: build with esbuild
# - job: assets
# -----------------------------------
## -- BEGIN building assets with esbuild
FROM compile_deps AS digest
WORKDIR /src
COPY assets/ ./assets
ENV MIX_ENV=prod
RUN mix assets.deploy
## -- END building assets with esbuild
<% end %>

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

WORKDIR /src

ENV MIX_ENV=prod

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

# uncomment if you have customized release config:
# COPY rel ./rel

RUN mix release --path /app --quiet

# -----------------------------------
# - stage: release
# - job: release_image
# -----------------------------------
FROM elixir-runner AS release_image

# If you need to inject the app revision into the container,
# uncomment below:
# ARG APP_REVISION=latest
# ENV APP_REVISION=${APP_REVISION}

ENV MIX_ENV=prod

USER nobody

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

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