feat: use release to deploy application

Adjust `Dockerfile` and `docker-compose` to use the `release` structure.
This commit is contained in:
João Paulo Dubas 2024-09-20 21:16:32 +00:00
parent 964985e328
commit 76d5fc9adf
No known key found for this signature in database
7 changed files with 109 additions and 6 deletions

View File

@ -1,10 +1,70 @@
FROM hexpm/elixir:1.17.2-erlang-27.0.1-debian-bookworm-20240701-slim AS builder
ARG ELIXIR_VERSION=1.17.2
ARG OTP_VERSION=27.0.1
ARG DEBIAN_VERSION=bookworm-20240701-slim
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
FROM ${BUILDER_IMAGE} AS builder
RUN apt-get update \
&& apt-get -y install git make
&& apt-get -y install \
build-essential \
git \
make \
&& apt-get clean \
&& rm -rf /var/lib/ap/lists/*_*
WORKDIR /opt/app
# install hex + rebar
RUN mix do local.hex --force, local.rebar --force
# set build ENV
ARG BUILD_MIX_ENV=prod
ENV MIX_ENV=${BUILD_MIX_ENV}
# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only ${MIX_ENV}
RUN mkdir config
# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${BUILD_MIX_ENV}.exs config/
RUN mix deps.compile
COPY priv priv
COPY lib lib
# Compile the release
RUN mix compile
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/
COPY rel rel
RUN mix release
COPY ./mix.exs ./
COPY ./mix.lock ./
RUN mix do local.hex --force, local.rebar --force \
&& mix do deps.get, deps.compile
EXPOSE 4000
ENTRYPOINT ["./priv/docker/service/docker-entrypoint.sh"]
CMD ["sample-cookie"]
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}
RUN apt-get update -y \
&& apt-get install -y \
ca-certificates \
libncurses5 \
libstdc++6 \
locales \
openssl \
tini \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# Set the locale
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
WORKDIR /opt/app
RUN chown nobody /opt/app
# set runner ENV
ARG BUILD_MIX_ENV=prod
ENV MIX_ENV=${BUILD_MIX_ENV}
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${BUILD_MIX_ENV}/rel/wabanex ./
USER nobody
EXPOSE 4000
ENTRYPOINT ["tini", "--"]
CMD ["/app/bin/server"]

View File

@ -17,6 +17,8 @@ services:
build:
target: builder
context: .
args:
BUILD_MIX_ENV: dev
hostname: app
depends_on:
- db

28
lib/wabanex/release.ex Normal file
View File

@ -0,0 +1,28 @@
defmodule Wabanex.Release do
@moduledoc """
Used for executing DB release tasks when run in production without Mix
installed.
"""
@app :wabanex
def migrate do
load_app()
for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end
def rollback(repo, version) do
load_app()
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
defp repos do
Application.fetch_env!(@app, :ecto_repos)
end
defp load_app do
Application.load(@app)
end
end

5
rel/overlays/bin/migrate Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd -P -- "$(dirname -- "$0")"
exec ./wabanex eval Wabanex.Release.migrate

1
rel/overlays/bin/migrate.bat Executable file
View File

@ -0,0 +1 @@
call "%~dp0\wabanex" eval Wabanex.Release.migrate

5
rel/overlays/bin/server Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd -P -- "$(dirname -- "$0")"
PHX_SERVER=true exec ./wabanex start

2
rel/overlays/bin/server.bat Executable file
View File

@ -0,0 +1,2 @@
set PHX_SERVER=true
call "%~dp0\wabanex" start