Skip to content

Instantly share code, notes, and snippets.

@mraaroncruz
Forked from teamon/Dockerfile
Created March 4, 2019 22:10
Show Gist options
  • Select an option

  • Save mraaroncruz/aa7ebb41e449831503bc73143f979c46 to your computer and use it in GitHub Desktop.

Select an option

Save mraaroncruz/aa7ebb41e449831503bc73143f979c46 to your computer and use it in GitHub Desktop.

Revisions

  1. @teamon teamon created this gist Aug 31, 2018.
    82 changes: 82 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    ########################################
    # 1. Build nodejs frontend
    ########################################
    FROM node:10.9-alpine as build-node

    # prepare build dir
    RUN mkdir -p /app/assets
    WORKDIR /app

    # set build ENV
    ENV NODE_ENV=prod

    # install npm dependencies
    COPY assets/package.json assets/package-lock.json ./assets/
    COPY deps/phoenix deps/phoenix
    COPY deps/phoenix_html deps/phoenix_html
    RUN cd assets && npm install

    # build assets
    COPY assets ./assets/
    RUN cd assets && npm run deploy


    ########################################
    # 2. Build elixir backend
    ########################################
    FROM elixir:1.6.6-alpine as build-elixir

    # install build dependencies
    RUN apk add --update git

    # prepare build dir
    RUN mkdir /app
    WORKDIR /app

    # install hex + rebar
    RUN mix local.hex --force && \
    mix local.rebar --force

    # set build ENV
    ENV MIX_ENV=prod

    # install dependencies
    COPY mix.exs mix.lock ./
    RUN mix deps.get

    # compile dependencies
    COPY config ./config/
    RUN mix deps.compile

    # copy only elixir files to keep the cache
    COPY lib ./lib/
    COPY priv ./priv/
    COPY rel ./rel/

    # copy assets from node build
    COPY --from=build-node /app/priv/static ./priv/static
    RUN mix phx.digest

    # build release
    RUN mix release --no-tar --verbose


    ########################################
    # 3. Build release image
    ########################################
    FROM alpine:3.8
    RUN apk add --update bash openssl

    RUN mkdir /app && chown -R nobody: /app
    WORKDIR /app
    USER nobody

    COPY --from=build-elixir /app/_build/prod/rel/myapp ./

    ARG VERSION
    ENV VERSION=$VERSION
    ENV REPLACE_OS_VARS=true
    EXPOSE 4000

    ENTRYPOINT ["/app/bin/myapp"]
    CMD ["foreground"]