Skip to content

Commit

Permalink
Merge pull request #94 from nimblehq/chore/refactor-docker-file
Browse files Browse the repository at this point in the history
Refactor the Dockerfile
  • Loading branch information
andyduong1920 authored Apr 16, 2021
2 parents 5636ea6 + c777ebb commit 2200d8f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 53 deletions.
10 changes: 6 additions & 4 deletions lib/nimble_template/addons/docker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ defmodule NimbleTemplate.Addons.Docker do
web_project?: web_project?,
otp_app: otp_app,
base_module: base_module,
docker_build_base_image: docker_build_base_image,
docker_app_base_image: docker_app_base_image
alpine_version: alpine_version,
elixir_version: elixir_version,
erlang_version: erlang_version
} = project,
_opts
) do
Expand All @@ -24,8 +25,9 @@ defmodule NimbleTemplate.Addons.Docker do
],
otp_app: otp_app,
base_module: base_module,
docker_build_base_image: docker_build_base_image,
docker_app_base_image: docker_app_base_image,
alpine_version: alpine_version,
elixir_version: elixir_version,
erlang_version: erlang_version,
web_project?: web_project?
)

Expand Down
7 changes: 2 additions & 5 deletions lib/nimble_template/project.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
defmodule NimbleTemplate.Project do
@moduledoc false

@alpine_version "3.13.2"
@elixir_version "1.11.4"
@erlang_version "23.3"
@node_version "14"

@alpine_version "3.13.2"

defstruct base_module: nil,
base_path: nil,
base_test_path: nil,
Expand All @@ -15,9 +14,7 @@ defmodule NimbleTemplate.Project do
web_path: nil,
web_test_path: nil,
# Dependency Versions
docker_app_base_image: "alpine:#{@alpine_version}",
docker_build_base_image:
"hexpm/elixir:#{@elixir_version}-erlang-#{@erlang_version}-alpine-#{@alpine_version}",
alpine_version: @alpine_version,
elixir_version: @elixir_version,
elixir_asdf_version:
"#{@elixir_version}-otp-#{@erlang_version |> String.split(".") |> List.first()}",
Expand Down
75 changes: 39 additions & 36 deletions priv/templates/nimble_template/Dockerfile.eex
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
FROM <%= docker_build_base_image %> AS build
ARG ELIXIR_IMAGE_VERSION=<%= elixir_version %>
ARG ERLANG_IMAGE_VERSION=<%= erlang_version %>
ARG RELEASE_IMAGE_VERSION=<%= alpine_version %>

FROM hexpm/elixir:${ELIXIR_IMAGE_VERSION}-erlang-${ERLANG_IMAGE_VERSION}-alpine-${RELEASE_IMAGE_VERSION} AS build

RUN apk update && \
apk upgrade --no-cache && \
apk add --no-cache \
nodejs \
npm \
git \
build-base && \
mix local.rebar --force && \
mix local.hex --force

# install build dependencies
RUN apk add --no-cache build-base npm git && \
mix local.hex --force && \
mix local.rebar --force

# prepare build dir
WORKDIR /app

# set build ENV
COPY . .

ENV MIX_ENV=prod

# install mix dependencies
COPY mix.exs mix.lock ./
COPY config config
RUN mix do deps.get, deps.compile
RUN mix do deps.get, deps.compile, compile
<%= if web_project? do %>
# 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
RUN cd assets && \
npm ci --progress=false --no-audit --loglevel=error && \
npm run deploy && \
cd - && \
mix phx.digest
<% end %>
# compile and build release
COPY lib lib
# uncomment COPY if rel/ exists
# COPY rel rel
RUN mix do compile, release

# prepare release image
FROM <%= docker_app_base_image %> AS app
RUN mix release

RUN apk add --no-cache openssl ncurses-libs
#
# Release
#
FROM alpine:${RELEASE_IMAGE_VERSION} AS app

WORKDIR /app
RUN apk update && \
apk add --no-cache \
bash \
openssl-dev

# Setup non-root user
RUN addgroup -S app_group && \
adduser -s /bin/sh -G app_group -D app_user && \
chown app_user:app_group /app
WORKDIR /opt/app
EXPOSE 4000

COPY --from=build --chown=app_user:app_group /app/_build/prod/rel/<%= otp_app %> ./
COPY bin/start.sh ./bin/start.sh
RUN addgroup -g 1000 appuser && \
adduser -u 1000 -G appuser -g appuser -s /bin/sh -D appuser && \
chown 1000:1000 /opt/app

ENV HOME=/app
COPY --from=build --chown=1000:1000 /app/_build/prod/rel/<%= otp_app %> ./
COPY bin/start.sh ./bin/start.sh

USER app_user

Expand Down
33 changes: 25 additions & 8 deletions test/nimble_template/addons/docker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,29 @@ defmodule NimbleTemplate.Addons.DockerTest do
Addons.Docker.apply(project)

assert_file("Dockerfile", fn file ->
assert file =~ "FROM hexpm/elixir:1.11.4-erlang-23.3-alpine-3.13.2 AS build"
assert file =~ "FROM alpine:3.13.2 AS app"
assert file =~ """
ARG ELIXIR_IMAGE_VERSION=1.11.4
ARG ERLANG_IMAGE_VERSION=23.3
ARG RELEASE_IMAGE_VERSION=3.13.2
assert file =~
"RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error"
FROM hexpm/elixir:${ELIXIR_IMAGE_VERSION}-erlang-${ERLANG_IMAGE_VERSION}-alpine-${RELEASE_IMAGE_VERSION} AS build
"""

assert file =~ "FROM alpine:${RELEASE_IMAGE_VERSION} AS app"

assert file =~ "adduser -s /bin/sh -G app_group -D app_user &&"
assert file =~ """
RUN cd assets && \\
\t\tnpm ci --progress=false --no-audit --loglevel=error && \\
\t\tnpm run deploy && \\
\t\tcd - && \\
\t\tmix phx.digest
"""

assert file =~ "adduser -u 1000 -G appuser -g appuser -s /bin/sh -D appuser"
assert file =~ "USER app_user"

assert file =~
"COPY --from=build --chown=app_user:app_group /app/_build/prod/rel/nimble_template ./"
"COPY --from=build --chown=1000:1000 /app/_build/prod/rel/nimble_template ./"
end)
end)
end
Expand Down Expand Up @@ -101,8 +113,13 @@ defmodule NimbleTemplate.Addons.DockerTest do
Addons.Docker.apply(project)

assert_file("Dockerfile", fn file ->
refute file =~
"RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error"
refute file =~ """
RUN cd assets && \\
\t\tnpm ci --progress=false --no-audit --loglevel=error && \\
\t\tnpm run deploy && \\
\t\tcd - && \\
\t\tmix phx.digest
"""
end)
end)
end
Expand Down

0 comments on commit 2200d8f

Please sign in to comment.