From 30bbc9b78ddd55f67347a37d8e77c6f346fa685e Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Fri, 13 Aug 2021 14:12:49 +0530 Subject: [PATCH] chore(build): create dockerfile for release deployment Signed-off-by: SphericalKat --- .dockerignore | 35 +++++++++++++++++ .gitignore | 3 +- Dockerfile | 50 ++++++++++++++++++++++++ config/prod.exs | 2 +- config/{prod.secret.exs => releases.exs} | 4 +- 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile rename config/{prod.secret.exs => releases.exs} (94%) diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c55d324 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,35 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where 3rd-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +ketbin-*.tar + +# If NPM crashes, it generates a log, let's ignore it too. +npm-debug.log + +# The directory NPM downloads your dependencies sources to. +/assets/node_modules/ + +# Since we are building assets from assets/, +# we ignore priv/static. You may want to comment +# this depending on your deployment strategy. +/priv/static/ +.idea \ No newline at end of file diff --git a/.gitignore b/.gitignore index c55d324..3ecd9ec 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,5 @@ npm-debug.log # we ignore priv/static. You may want to comment # this depending on your deployment strategy. /priv/static/ -.idea \ No newline at end of file +.idea +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a29b2ef --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +FROM elixir:alpine AS build + +# install build dependencies +RUN apk add --no-cache build-base npm git curl py-pip + +# prepare build dir +WORKDIR /app + +# install hex + rebar +RUN mix local.hex --force && \ + mix local.rebar --force + +# set build ENV +ENV MIX_ENV=prod + +# install mix dependencies +COPY mix.exs mix.lock ./ +COPY config config +RUN mix do deps.get, deps.compile + +# build assets +COPY assets/package.json assets/package-lock.json ./assets/ +RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error + +COPY priv priv +COPY assets assets +RUN npm run --prefix ./assets deploy +RUN mix phx.digest + +# compile and build release +COPY lib lib +# uncomment COPY if rel/ exists +# COPY rel rel +RUN mix do compile, release + +# prepare release image +FROM alpine:3.9 AS app +RUN apk add --no-cache openssl ncurses-libs libstdc++ + +WORKDIR /app + +RUN chown nobody:nobody /app + +USER nobody:nobody + +COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/ketbin ./ + +ENV HOME=/app + +CMD ["bin/ketbin", "start"] \ No newline at end of file diff --git a/config/prod.exs b/config/prod.exs index dbc6ccf..8325462 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -52,4 +52,4 @@ config :logger, level: :info # Finally import the config/prod.secret.exs which loads secrets # and configuration from environment variables. -import_config "prod.secret.exs" +# import_config "prod.secret.exs" diff --git a/config/prod.secret.exs b/config/releases.exs similarity index 94% rename from config/prod.secret.exs rename to config/releases.exs index 09cb0ec..716bf58 100644 --- a/config/prod.secret.exs +++ b/config/releases.exs @@ -2,7 +2,7 @@ # from environment variables. You can also hardcode secrets, # although such is generally not recommended and you have to # remember to add this file to your .gitignore. -use Mix.Config +import Config database_url = System.get_env("DATABASE_URL") || @@ -35,7 +35,7 @@ config :ketbin, KetbinWeb.Endpoint, # If you are doing OTP releases, you need to instruct Phoenix # to start each relevant endpoint: # -# config :ketbin, KetbinWeb.Endpoint, server: true +config :ketbin, KetbinWeb.Endpoint, server: true # # Then you can assemble a release by calling `mix release`. # See `mix help release` for more information.