Skip to content

Commit

Permalink
fix: update Dockerfile and Fly toml files
Browse files Browse the repository at this point in the history
  • Loading branch information
w3b6x9 committed Jul 12, 2022
1 parent f1d629b commit 3f3c6e8
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prod_linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup elixir
uses: erlef/setup-beam@v1
with:
otp-version: 23.x # Define the OTP version [required]
otp-version: 25.x # Define the OTP version [required]
elixir-version: 1.13.x # Define the elixir version [required]
- name: Cache Mix
uses: actions/cache@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staging_linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup elixir
uses: erlef/setup-beam@v1
with:
otp-version: 23.x # Define the OTP version [required]
otp-version: 25.x # Define the OTP version [required]
elixir-version: 1.13.x # Define the elixir version [required]
- name: Cache Mix
uses: actions/cache@v1
Expand Down
107 changes: 63 additions & 44 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
###
### Fist Stage - Building the Release
###
FROM hexpm/elixir:1.13.4-erlang-23.2.7.5-alpine-3.14.6 AS build
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian instead of
# Alpine to avoid DNS resolution issues in production.
#
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
# https://hub.docker.com/_/ubuntu?tab=tags
#
#
# This file is based on these images:
#
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20210902-slim - for the release image
# - https://pkgs.org/ - resource for finding needed packages
# - Ex: hexpm/elixir:1.13.4-erlang-25.0-debian-bullseye-20210902-slim
#
ARG ELIXIR_VERSION=1.13.4
ARG OTP_VERSION=25.0
ARG DEBIAN_VERSION=bullseye-20210902-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

# install build dependencies
RUN apk add --no-cache build-base npm git
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app
Expand All @@ -13,61 +32,61 @@ WORKDIR /app
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV as prod
ENV MIX_ENV=prod
# set build ENV
ENV MIX_ENV="prod"

# Copy over the mix.exs and mix.lock files to load the dependencies. If those
# files don't change, then we don't keep re-fetching and rebuilding the deps.
# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

RUN mix deps.get --only prod && \
mix deps.compile

# install npm dependencies
# COPY assets/package.json assets/package-lock.json ./assets/
# RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error
# 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/${MIX_ENV}.exs config/
RUN mix deps.compile

COPY priv priv
# COPY assets assets

# NOTE: If using TailwindCSS, it uses a special "purge" step and that requires
# the code in `lib` to see what is being used. Uncomment that here before
# running the npm deploy script if that's the case.
COPY lib lib

# build assets
# RUN npm run --prefix ./assets deploy
RUN mix phx.digest
COPY assets assets

# Compile the release
RUN mix compile

# copy source here if not using TailwindCSS
# COPY lib lib
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/

# compile and build release
COPY rel rel
RUN mix do compile, release
RUN mix release

###
### Second Stage - Setup the Runtime Environment
###
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}

# prepare release docker image
FROM alpine:3.13.3 AS app
RUN apk add --no-cache libstdc++ openssl ncurses-libs
RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

WORKDIR /app
# 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

RUN chown nobody:nobody /app
WORKDIR "/app"
RUN chown nobody /app

# USER root
# set runner ENV
ENV MIX_ENV="prod"

COPY --from=build --chown=nobody:nobody /app/_build/prod/rel/realtime ./
# COPY --from=build /app/_build/prod/rel/realtime ./
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/realtime ./

ENV HOME=/app
ENV MIX_ENV=prod
USER nobody

COPY limits.sh ./limits.sh
# RUN chmod +x /app/limits.sh
ENTRYPOINT ["/app/limits.sh"]
CMD ["bin/realtime", "start"]
CMD ["/app/bin/server"]
# Appended by flyctl
ENV ECTO_IPV6 true
ENV ERL_AFLAGS "-proto_dist inet6_tcp"
15 changes: 8 additions & 7 deletions deploy/fly/prod.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
app = "realtime-prod"

kill_signal = "SIGTERM"
kill_timeout = 5
processes = []

[deploy]
release_command = "/app/bin/realtime eval Realtime.Release.migrate"
strategy = "rolling"

[env]
SECURE_CHANNELS = "true"
DNS_NODES = "realtime-prod.internal"

[experimental]
private_network = true
allowed_public_ports = []
auto_rollback = true

[[services]]
http_checks = []
internal_port = 4000
processes = ["app"]
protocol = "tcp"

script_checks = []
[services.concurrency]
hard_limit = 100000
soft_limit = 100000
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

Expand Down
17 changes: 9 additions & 8 deletions deploy/fly/staging.toml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
app = "realtime-staging"

kill_signal = "SIGTERM"
kill_timeout = 5
processes = []

[deploy]
strategy = "rolling"
release_command = "/app/bin/realtime eval Realtime.Release.migrate"

[env]
SECURE_CHANNELS = "true"
DNS_NODES = "realtime-staging.internal"
strategy = "rolling"

[experimental]
private_network = true
allowed_public_ports = []
auto_rollback = true

[[services]]
http_checks = []
internal_port = 4000
processes = ["app"]
protocol = "tcp"

script_checks = []
[services.concurrency]
hard_limit = 100000
soft_limit = 100000
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

Expand Down
2 changes: 1 addition & 1 deletion lib/realtime_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule RealtimeWeb.Router do
Realtime.Repo.Replica.IAD,
Realtime.Repo.Replica.SIN
],
ecto_psql_extras_options: [long_running_queries: [threshold: "400 milliseconds"]],
ecto_psql_extras_options: [long_running_queries: [threshold: "200 milliseconds"]],
metrics: RealtimeWeb.Telemetry
end

Expand Down
3 changes: 3 additions & 0 deletions rel/overlays/bin/migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
cd -P -- "$(dirname -- "$0")"
exec ./realtime eval Realtime.Release.migrate
1 change: 1 addition & 0 deletions rel/overlays/bin/migrate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
call "%~dp0\realtime" eval Realtime.Release.migrate
3 changes: 3 additions & 0 deletions rel/overlays/bin/server
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
cd -P -- "$(dirname -- "$0")"
PHX_SERVER=true exec ./realtime start
2 changes: 2 additions & 0 deletions rel/overlays/bin/server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set PHX_SERVER=true
call "%~dp0\realtime" start

0 comments on commit 3f3c6e8

Please sign in to comment.