Last active
May 11, 2022 03:16
-
-
Save zrod/37ac53d3a3d19aa794b3a5432079518c to your computer and use it in GitHub Desktop.
Optimized dockerfile for Rails
This file contains hidden or 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
# Main stage responsible for installing and configuring Rails and assets dependencies | |
FROM ruby:3.0.4-alpine AS ruby-base | |
ARG RAILS_MASTER_KEY | |
ARG BUNDLER_VERSION=2.3.12 | |
ENV RAILS_MASTER_KEY=${RAILS_MASTER_KEY} | |
ENV RAILS_ENV=production | |
ENV NODE_ENV=production | |
# Install system packages required by gems (timezone update may be skipped here) | |
RUN apk update && apk add \ | |
build-base \ | |
postgresql-dev \ | |
nodejs \ | |
npm \ | |
tzdata \ | |
gcompat \ | |
&& cp /usr/share/zoneinfo/America/Toronto /etc/localtime \ | |
&& rm -rf /var/cache/apk/* | |
RUN gem install bundler -v ${BUNDLER_VERSION} \ | |
&& npm install -g yarn | |
# See https://bundler.io/v2.3/guides/bundler_docker_guide.html | |
ENV GEM_HOME="/app/vendor/bundle/ruby/3.0.0" | |
ENV PATH=$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH | |
WORKDIR /app | |
COPY Gemfile Gemfile.lock yarn.lock ./ | |
# Configure bundler to install dependencies without dev and test to app/vendor | |
RUN bundle config set --local deployment true \ | |
&& bundle config set --local without development test \ | |
&& bundle install --jobs 4 | |
COPY . . | |
# Update binstubs so that app/bin references the correct gems under app/vendor + precompile webpacker assets | |
RUN yarn install --check-files --production \ | |
&& rake app:update:bin \ | |
&& rails webpacker:binstubs \ | |
&& RAILS_MASTER_KEY=${RAILS_MASTER_KEY} bundle exec rails assets:precompile \ | |
&& rm -rf node_modules tmp/cache vendor/assets | |
# Second and final build stage | |
FROM ruby:3.0.4-alpine | |
ARG USERNAME=concierge | |
ARG USER_UID=1000 | |
ARG USER_GID=$USER_UID | |
ARG BUNDLER_VERSION=2.3.12 | |
# Install system packages required by some gems to be able to run, updates timezone and add a custom user to run the container | |
RUN apk update && apk add \ | |
nodejs \ | |
libpq \ | |
tzdata \ | |
gcompat \ | |
&& cp /usr/share/zoneinfo/America/Toronto /etc/localtime \ | |
&& rm -rf /var/cache/apk/* \ | |
&& addgroup -g ${USER_GID} ${USERNAME} \ | |
&& adduser -D ${USERNAME} -u ${USER_UID} -G ${USERNAME} | |
ENV RAILS_ENV=production | |
ENV GEM_HOME="/app/vendor/bundle/ruby/3.0.0" | |
ENV PATH=$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH | |
WORKDIR /app | |
# Copy /app with dependencies already installed and assets compiled, ownership set to our custom user | |
COPY --from=ruby-base --chown=${USERNAME}:${USERNAME} /app ./ | |
# Sets additional bundler configuration so it knows where/what to read from + updates binstubs | |
RUN gem install bundler -v ${BUNDLER_VERSION} \ | |
&& bundle config --local path vendor/bundle \ | |
&& bundle config --local without development test \ | |
&& rake app:update:bin | |
# instructs docker to run the container as the user created a few lines above | |
USER ${USERNAME} | |
LABEL maintainer="Rodrigo Z.A." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment