Current section
Files
Jump to
Current section
Files
lib/mix/tasks/slipway.gen.docker.ex
defmodule Mix.Tasks.Slipway.Gen.Docker do
@moduledoc "Creates a template docker file"
@shortdoc "Generates a template docker file"
use Mix.Task
@impl Mix.Task
def run(_) do
name = Mix.Project.get()
app = Mix.Project.config[:app]
elixir_version = Mix.Project.config[:elixir] |> String.replace("~> ", "")
if name == nil do
Mix.shell().error("No mix.exs found, must be run in an Elixir project")
else
Mix.Generator.create_file("Dockerfile", contents(app, elixir_version))
Mix.shell().info(
"""
You need to run the following to build the docker image:
docker build -t elixir-#{app} .
Then eithr
docker run --rm elixir-#{app}
or
docker run -it --rm elixir-#{app}
""")
end
end
defp contents(app_name, elixir_version) do
"""
#===========
#Build Stage
#===========
FROM bitwalker/alpine-elixir:#{elixir_version} as build
ARG BUILD_ENV=prod
ARG BUILD_REL=#{app_name}
#Copy the source folder into the Docker image
COPY . .
ENV MIX_ENV=${BUILD_ENV}
#Install dependencies and build Release
RUN mix local.hex --force && \
mix local.rebar --force && \
mix deps.get && \
mix compile && \
mix release --path /export
# #================
# #Deployment Stage
# #================
FROM pentacent/alpine-erlang-base:latest
ARG BUILD_REL=#{app_name}
RUN apk add --no-cache openssl ncurses-libs libstdc++
#Copy release file from the previous stage
COPY --from=build /export/ .
#Set environment variables and expose port
EXPOSE 4000
ENV REPLACE_OS_VARS=true \
PORT=4000
ENV RELEASE_DISTRIBUTION="name"
# This value should be overriden at runtime
ENV RELEASE_IP="127.0.0.1"
# This will be the basename of our node
ENV RELEASE_NAME="${BUILD_REL}"
# This will be the full nodename
ENV RELEASE_NODE="${RELEASE_NAME}@${RELEASE_IP}"
# If empty, the default cookie generated by `mix release` will be used
# OVERRIDE IT!!
ENV RELEASE_COOKIE=""
# #Change user
USER default
#Set default entrypoint and command
ENTRYPOINT ["bin/#{app_name}", "start"]
CMD ["foreground"]
"""
end
end