Skip to content

Instantly share code, notes, and snippets.

@bsedat
Last active August 8, 2023 05:56
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bsedat/16cb74ebc8ab0ed61ac598a129b0a7ea to your computer and use it in GitHub Desktop.
Save bsedat/16cb74ebc8ab0ed61ac598a129b0a7ea to your computer and use it in GitHub Desktop.
Elixir Phoenix Umbrella App + Distillery Multistage Docker Build
FROM elixir:1.4.5 as asset-builder-mix-getter
ENV HOME=/opt/app
RUN mix do local.hex --force, local.rebar --force
# Cache elixir deps
COPY config/ $HOME/config/
COPY mix.exs mix.lock $HOME/
COPY apps/myproject_web/mix.exs $HOME/apps/myproject_web/
COPY apps/myproject_web/config/ $HOME/apps/myproject_web/config/
WORKDIR $HOME/apps/myproject_web
RUN mix deps.get
########################################################################
FROM node:6 as asset-builder
ENV HOME=/opt/app
WORKDIR $HOME
COPY --from=asset-builder-mix-getter $HOME/deps $HOME/deps
WORKDIR $HOME/apps/myproject_web/assets
COPY apps/myproject_web/assets/ ./
RUN yarn install
RUN ./node_modules/.bin/brunch build --production
########################################################################
FROM bitwalker/alpine-elixir:1.4.5 as releaser
ENV HOME=/opt/app
ARG ERLANG_COOKIE
ENV ERLANG_COOKIE $ERLANG_COOKIE
# dependencies for comeonin
RUN apk add --no-cache build-base cmake
# Install Hex + Rebar
RUN mix do local.hex --force, local.rebar --force
# Cache elixir deps
COPY config/ $HOME/config/
COPY mix.exs mix.lock $HOME/
# Copy umbrella app config + mix.exs files
COPY apps/myproject/mix.exs $HOME/apps/myproject/
COPY apps/myproject/config/ $HOME/apps/myproject/config/
COPY apps/myproject_web/mix.exs $HOME/apps/myproject_web/
COPY apps/myproject_web/config/ $HOME/apps/myproject_web/config/
ENV MIX_ENV=prod
RUN mix do deps.get --only $MIX_ENV, deps.compile
COPY . $HOME/
# Digest precompiled assets
COPY --from=asset-builder $HOME/apps/myproject_web/priv/static/ $HOME/apps/myproject_web/priv/static/
WORKDIR $HOME/apps/myproject_web
RUN mix phx.digest
# Release
WORKDIR $HOME
RUN mix release --env=$MIX_ENV --verbose
########################################################################
FROM alpine:3.6
ENV LANG=en_US.UTF-8 \
HOME=/opt/app/ \
TERM=xterm
ENV MYPROJECT_VERSION=0.1.0
RUN apk add --no-cache ncurses-libs openssl
EXPOSE 4000
ENV PORT=4000 \
MIX_ENV=prod \
REPLACE_OS_VARS=true \
SHELL=/bin/sh
COPY --from=releaser $HOME/_build/prod/rel/myproject/releases/$MYPROJECT_VERSION/myproject.tar.gz $HOME
WORKDIR $HOME
RUN tar -xzf myproject.tar.gz
ENTRYPOINT ["/opt/app/bin/myproject"]
CMD ["foreground"]
@shdblowers
Copy link

thanks for sharing this. used it as a basis for my own Dockerfile for Elixir umbrella app.

I have a question, why did you copy over the config files before running RUN mix do deps.get --only $MIX_ENV, deps.compile? Just curious because I tried it without copying over the config and everything worked fine.

@fabienengels
Copy link

I have a question, why did you copy over the config files before running RUN mix do deps.get --only $MIX_ENV, deps.compile? Just curious because I tried it without copying over the config and everything worked fine.

It allows you to cache the dependencies of your project and not rebuild them every single time your code change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment