From 579cc97babb748ee8c1d39ae95751d8725d11b2d Mon Sep 17 00:00:00 2001 From: danilo silva Date: Mon, 8 Jun 2020 21:01:05 +0000 Subject: [PATCH] build: Dockerized application --- .dockerignore | 36 ++++++++++++++++++++++++++++ Dockerfile | 58 +++++++++++++++++++++++++++++++++++++++++++++ config/prod.exs | 4 ++-- config/releases.exs | 10 ++++++++ mix.exs | 2 +- 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 config/releases.exs diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c7185d9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,36 @@ +# 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"). +podcast_feed-*.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. +rel/ +metadata.json +.buildpacks diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8e271cd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,58 @@ +# ---- Build Stage ---- +FROM erlang:21 AS app_builder + +# Set environment variables for building the application +ENV MIX_ENV=prod \ + TEST=1 \ + LANG=C.UTF-8 + +# Fetch the latest version of Elixir (once the 1.9 docker image is available you won't have to do this) +RUN set -xe \ + && ELIXIR_DOWNLOAD_URL="https://github.com/elixir-lang/elixir/archive/v1.10.3.tar.gz" \ + && ELIXIR_DOWNLOAD_SHA256="f3035fc5fdade35c3592a5fa7c8ee1aadb736f565c46b74b68ed7828b3ee1897" \ + && curl -fSL -o elixir-src.tar.gz $ELIXIR_DOWNLOAD_URL \ + && echo "$ELIXIR_DOWNLOAD_SHA256 elixir-src.tar.gz" | sha256sum -c - \ + && mkdir -p /usr/local/src/elixir \ + && tar -xzC /usr/local/src/elixir --strip-components=1 -f elixir-src.tar.gz \ + && rm elixir-src.tar.gz \ + && cd /usr/local/src/elixir \ + && make install clean + +# Install hex and rebar +RUN mix local.hex --force && \ + mix local.rebar --force + +# Create the application build directory +RUN mkdir /app +WORKDIR /app + +# Copy over all the necessary application files and directories +COPY config ./config +COPY lib ./lib +COPY priv ./priv +COPY mix.exs . +COPY mix.lock . + +# Fetch the application dependencies and build the application +RUN mix deps.get +RUN mix deps.compile +RUN mix phx.digest +RUN mix release + +# ---- Application Stage ---- +FROM debian:stretch AS app + +ENV LANG=C.UTF-8 + +# Install openssl +RUN apt-get update && apt-get install -y openssl + +# Copy over the build artifact from the previous step and create a non root user +RUN useradd --create-home app +WORKDIR /home/app +COPY --from=app_builder /app/_build . +RUN chown -R app: ./prod +USER app + +# Run the Phoenix app +CMD ["./prod/rel/podcast_feed/bin/podcast_feed", "start"] diff --git a/config/prod.exs b/config/prod.exs index 0374bab..0cf94b1 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -16,8 +16,8 @@ config :podcast_feed, PodcastFeedWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json", server: true, root: ".", - version: Application.spec(:phoenix_distillery, :vsn), - force_ssl: [rewrite_on: [:x_forwarded_proto]] + version: Application.spec(:phoenix_distillery, :vsn) + # force_ssl: [rewrite_on: [:x_forwarded_proto]] config :podcast_feed, cache_duration: :timer.hours(24) diff --git a/config/releases.exs b/config/releases.exs new file mode 100644 index 0000000..56f2c8c --- /dev/null +++ b/config/releases.exs @@ -0,0 +1,10 @@ +import Config + +secret_key_base = System.fetch_env!("SECRET_KEY_BASE") +application_port = System.fetch_env!("APP_PORT") +application_host = System.fetch_env!("APP_HOST") + +config :podcast_feed, PodcastFeedWeb.Endpoint, + url: [scheme: "http", host: application_host, port: String.to_integer(application_port)], + http: [:inet6, port: String.to_integer(application_port)], + secret_key_base: secret_key_base diff --git a/mix.exs b/mix.exs index 471af45..9861253 100644 --- a/mix.exs +++ b/mix.exs @@ -55,7 +55,7 @@ defmodule PodcastFeed.MixProject do [ {:credo, "~> 1.4", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 1.0", only: [:dev], runtime: false}, - {:excoveralls, "~> 0.7", only: [:dev, :test]} + {:excoveralls, "~> 0.7", only: [:dev, :test]}, ] end