Skip to content

Instantly share code, notes, and snippets.

@fernandes
Last active June 20, 2020 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandes/348a10841696f87ae1ea92e09d7ba105 to your computer and use it in GitHub Desktop.
Save fernandes/348a10841696f87ae1ea92e09d7ba105 to your computer and use it in GitHub Desktop.
Docker multi stage build for Lucky Framework app
FROM crystallang/crystal:0.34.0-alpine-build AS build-env
ARG LUCKY_ROOT=/app
ARG DEV_PACKAGES="nodejs yarn"
ENV LUCKY_ENV=production
ENV NODE_ENV=production
WORKDIR $LUCKY_ROOT
# install packages
RUN apk update \
&& apk add --update --no-cache $DEV_PACKAGES
COPY shard* package.json yarn.lock ./
# shards, yarn, compile assets and crystal build are in different commands
# because docker will create different layers and cache it
# so when you build again, if nothing was changed, it will build blazing fast
# and in case only js or crystal code is changed, will run only its part
# install shards
COPY shard* $LUCKY_ROOT/
RUN shards
RUN yarn install --production=false
COPY . .
RUN yarn prod
RUN crystal build --release --static src/start_server.cr
# Remove folders not needed in resulting image
# rm does not result into smaller docker layer image
# I'm leaving as reference
# RUN rm -rf bin config db lib node_modules script spec src tasks
############### Build step done ###############
FROM alpine
ARG LUCKY_ROOT=/app
ENV LUCKY_ENV=production
WORKDIR $LUCKY_ROOT
COPY --from=build-env $LUCKY_ROOT/start_server $LUCKY_ROOT
COPY --from=build-env $LUCKY_ROOT/public $LUCKY_ROOT/public
EXPOSE 3000
CMD ["./start_server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment