Build docker container with release

This commit is contained in:
Adrian Kumpf
2019-04-06 23:49:55 +02:00
parent 7b314e2d79
commit 9a11c4b7e7
6 changed files with 97 additions and 3 deletions

10
.dockerignore Normal file
View File

@@ -0,0 +1,10 @@
_build/
deps/
.git/
.gitignore
Dockerfile
Makefile
README*
test/
priv/static/
**/node_modules

1
.gitignore vendored
View File

@@ -40,3 +40,4 @@ npm-debug.log
# secrets files as long as you replace their contents by environment
# variables.
/config/*.secret.exs
config/*.env

64
Dockerfile Normal file
View File

@@ -0,0 +1,64 @@
FROM elixir:1.8-alpine AS builder
ARG APP_NAME
ARG APP_VSN
ENV APP_NAME=${APP_NAME} \
APP_VSN=${APP_VSN} \
MIX_ENV=prod
WORKDIR /opt/app
RUN apk update && \
apk upgrade --no-cache && \
apk add --no-cache nodejs yarn git build-base && \
mix local.rebar --force && \
mix local.hex --force
COPY mix.* ./
RUN mix do deps.get --only $MIX_ENV, deps.compile
COPY assets assets
RUN cd assets && \
yarn install && \
yarn deploy && \
cd ..
COPY . .
RUN mix do phx.digest, compile
RUN \
mkdir -p /opt/built && \
mix release && \
cp _build/${MIX_ENV}/rel/${APP_NAME}/releases/${APP_VSN}/${APP_NAME}.tar.gz /opt/built && \
cd /opt/built && \
tar -xzf ${APP_NAME}.tar.gz && \
rm ${APP_NAME}.tar.gz
########################################################################
FROM alpine:3.9
ENV LANG=en_US.UTF-8 \
TZ=Europe/Berlin \
TERM=xterm
ARG APP_NAME
RUN apk update && \
apk --no-cache upgrade && \
apk add --no-cache bash openssl-dev erlang-crypto
ENV REPLACE_OS_VARS=true \
APP_NAME=${APP_NAME}
WORKDIR /opt/app
RUN addgroup -g 4005 $APP_NAME && \
adduser -u 4005 -D -h . -G $APP_NAME $APP_NAME
USER $APP_NAME
COPY --from=builder /opt/built .
CMD trap 'exit' INT; /opt/app/bin/${APP_NAME} foreground

20
Makefile Normal file
View File

@@ -0,0 +1,20 @@
.PHONY: help
APP_NAME ?= `grep 'app:' mix.exs | sed -e 's/\[//g' -e 's/ //g' -e 's/app://' -e 's/[:,]//g'`
APP_VSN ?= `grep 'version:' mix.exs | cut -d '"' -f2`
BUILD ?= `git rev-parse --short HEAD`
help:
@echo "$(APP_NAME):$(APP_VSN)-$(BUILD)"
@perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
build: ## Build the Docker image
@docker build --build-arg APP_NAME=$(APP_NAME) \
--build-arg APP_VSN=$(APP_VSN) \
-t $(APP_NAME):$(APP_VSN)-$(BUILD) \
-t $(APP_NAME):latest .
run: ## Run the app in Docker
@docker run --env-file config/docker.env \
--expose 4000 -p 4000:4000 \
--rm -it $(APP_NAME):latest

View File

@@ -45,8 +45,7 @@ defmodule TeslaMate.MixProject do
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
# Custom
# {:tesla_api, git: "https://github.com/adriankumpf/tesla_api", branch: "ak/error-handling"},
{:tesla_api, path: "/Users/adrian/dev/projects/tesla_api"},
{:tesla_api, git: "https://github.com/adriankumpf/tesla_api", branch: "ak/error-handling"},
{:gen_state_machine, "~> 2.0"},
{:ecto_enum, "~> 1.2"},
{:distillery, "~> 2.0"},

View File

@@ -35,6 +35,6 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.3.0", "099a7f3ce31e4780f971b4630a3c22ec66d22208bc090fe33a2a3a6a67754a73", [:rebar3], [], "hexpm"},
"tesla": {:hex, :tesla, "1.2.1", "864783cc27f71dd8c8969163704752476cec0f3a51eb3b06393b3971dc9733ff", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"},
"tesla_api": {:git, "https://github.com/adriankumpf/tesla_api", "4432bb8f2684b2779a058c3f52e3418a89907319", [branch: "ak/error-handling"]},
"tesla_api": {:git, "https://github.com/adriankumpf/tesla_api", "62cdc6559cc9ee092e2793b463bebf464e912cf7", [branch: "ak/error-handling"]},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}