Last active
May 25, 2021 16:50
-
-
Save camallen/1befc73dd0524156fe7f6844b3113b7f to your computer and use it in GitHub Desktop.
rails app multi stage docker builds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start from a small, trusted base image with the version pinned down | |
FROM ruby:2.5-slim-stretch AS base | |
# Install system dependencies required both at runtime and build time | |
# The image uses Postgres but you can swap it with mariadb-dev (for MySQL) or sqlite-dev | |
RUN apt-get update && \ | |
apt-get install --no-install-recommends -y \ | |
build-essential \ | |
# git is required for installing gems from git repos | |
git \ | |
libpq-dev \ | |
tmpreaper \ | |
# libjemalloc1 (v3) provides big memory savings vs jemalloc v5+ (default on debian buster) | |
libjemalloc1 \ | |
tmpreaper \ | |
&& \ | |
apt-get clean && rm -rf /var/lib/apt/lists/* | |
ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.1 | |
FROM base AS builder | |
WORKDIR /rails_app | |
ADD ./Gemfile /rails_app/ | |
ADD ./Gemfile.lock /rails_app/ | |
RUN bundle config --global jobs `cat /proc/cpuinfo | grep processor | wc -l | xargs -I % expr % - 1` | |
RUN bundle install --without development test | |
ARG REVISION='' | |
ENV REVISION=$REVISION | |
RUN (cd /rails_app && echo $REVISION > commit_id.txt) | |
RUN (cd /rails_app && mkdir -p tmp/pids && rm -f tmp/pids/*.pid) | |
# debian stretch has libjemalloc1 https://packages.debian.org/stretch/libjemalloc1 | |
FROM base | |
WORKDIR /rails_app | |
# We copy over the entire gems directory for our builder image, containing the already built artifact | |
COPY --from=builder /usr/local/bundle/ /usr/local/bundle/ | |
COPY --from=builder /rails_app/ /rails_app/ | |
# set a default RAILS_ENV for the build scripts | |
# this is required for the `rake assets:precompile` script | |
# to write assets to target dir set in `config.assets.prefix` | |
ARG RAILS_ENV=production | |
ENV RAILS_ENV=$RAILS_ENV | |
ADD ./ /rails_app | |
RUN (cd /rails_app && SECRET_KEY_BASE=1a bundle exec rake assets:precompile) | |
EXPOSE 81 | |
CMD ["/rails_app/scripts/docker/start.sh"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment