Mutli-stage build for production

This commit is contained in:
prichier
2020-10-22 15:19:01 +02:00
parent e945f48665
commit 1acff2dd8a
8 changed files with 167 additions and 172 deletions

View File

@@ -1,62 +1,41 @@
FROM elixir:slim
# First build the application assets
FROM node:alpine as assets
# Install dependencies, NodeJS, YARN & clean apt
RUN apt update \
&& apt -y dist-upgrade \
&& apt -y install build-essential \
curl \
wget \
unzip \
vim \
openssl \
git \
cmake \
imagemagick \
webp \
gifsicle \
jpegoptim \
optipng \
pngquant \
postgresql-client \
&& curl -sL https://deb.nodesource.com/setup_12.x | bash - \
&& apt -y install nodejs \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
&& apt -y update && apt -y install yarn \
&& apt -y clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN apk add --no-cache python build-base
# Add mobilizon user
RUN groupadd -r mobilizon \
&& useradd -r -g mobilizon -m mobilizon
COPY js .
RUN yarn install \
&& yarn run build
USER mobilizon
# Then, build the application binary
FROM elixir:alpine AS builder
# ENV
RUN apk add --no-cache build-base git cmake
COPY mix.exs mix.lock ./
ENV MIX_ENV=prod
RUN mix local.hex --force \
&& mix local.rebar --force \
&& mix deps.get
# PORT
COPY lib ./lib
COPY priv ./priv
COPY config ./config
COPY docker/production/releases.exs ./config/
COPY --from=assets ./priv/static ./priv/static
RUN mix phx.digest \
&& mix release
# Finally setup the app
FROM alpine
RUN apk add --no-cache openssl ncurses-libs
USER nobody
EXPOSE 4000
# Copy repo
COPY . /app
WORKDIR /app
COPY --from=builder --chown=nobody:nobody _build/prod/rel/mobilizon ./
# Compile dependencies, Mobilizon and build front-end
RUN mix local.hex --force && mix local.rebar --force \
&& HEX_HTTP_CONCURRENCY=4 HEX_HTTP_TIMEOUT=60 mix do deps.get, compile \
&& cd js && NODE_BUILD_MEMORY=2024 yarn install && NODE_BUILD_MEMORY=2024 yarn run build \
# free space
&& rm -rf js doc deps docs support \
&& rm -rf /home/mobilizon/.cache/* \
# copy config secret env based file
&& cp docker/production/prod.secret config/ \
# set start script mod
&& chmod +x /app/docker/production/start.sh
CMD /app/docker/production/start.sh
## start.sh:
# !/bin/bash
# mix ecto.migrate
# mix phx.server
ENTRYPOINT ["/bin/mobilizon"]
CMD ["start"]