Skip to content

Instantly share code, notes, and snippets.

@linkdd
Created Oct 19, 2021
Embed
What would you like to do?
Example Dockerfile for Elixir/Phoenix project
## Layers containing files for the next stages
# Project configuration
FROM scratch AS context
ADD mix.exs mix.lock /workspace/
ADD config /workspace/config
# Asset files
FROM scratch AS assets
ADD priv /workspace/priv
ADD assets /workspace/assets
# Source code
FROM scratch AS sources
ADD lib /workspace/lib
## Layers used to create the next stages
# Build agent
FROM hexpm/elixir:1.12.1-erlang-24.0.1-alpine-3.13.3 AS build-agent
RUN apk add --no-cache build-base npm
RUN mix local.hex --force
RUN mix local.rebar --force
# Runner agent
FROM alpine:latest AS runner-agent
RUN apk add --no-cache openssl ncurses-libs libgcc libstdc++
## Build pipeline stages
# Fetch and build dependencies
FROM build-agent AS dependencies
ARG MIX_ENV=prod
COPY --from=context /workspace /workspace
WORKDIR /workspace
RUN MIX_ENV=$MIX_ENV mix do deps.get, deps.compile
# Build assets
FROM build-agent AS gen-assets
ARG MIX_ENV=prod
COPY --from=context /workspace /workspace
COPY --from=assets /workspace/priv /workspace/priv
COPY --from=assets /workspace/assets /workspace/assets
COPY --from=dependencies /workspace/deps /workspace/deps
COPY --from=dependencies /workspace/_build /workspace/_build
WORKDIR /workspace
RUN MIX_ENV=$MIX_ENV mix assets.deploy
# Build release
FROM build-agent AS gen-release
ARG MIX_ENV=prod
COPY --from=context /workspace /workspace
COPY --from=dependencies /workspace/deps /workspace/deps
COPY --from=dependencies /workspace/_build /workspace/_build
COPY --from=gen-assets /workspace/priv /workspace/priv
COPY --from=sources /workspace/lib /workspace/lib
WORKDIR /workspace
RUN MIX_ENV=$MIX_ENV mix do compile, release
## Final artifact
FROM runner-agent AS runner
ARG MIX_ENV=prod
ARG RELEASE=my-app
COPY --from=gen-release /workspace/_build/${MIX_ENV}/rel/${RELEASE} /opt/${RELEASE}
ENV DATABASE_URL=""
ENV POOL_SIZE="10"
ENV SECRET_KEY_BASE=""
ENV PORT="4000"
ENTRYPOINT ["/opt/${RELEASE}/bin/${RELEASE}"]
CMD ["start"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment