2023-03-24 11:50:11 -07:00
|
|
|
# syntax=docker/dockerfile:1
|
2022-04-24 22:52:31 -07:00
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
FROM ruby:3.2.1-slim as base
|
|
|
|
|
|
|
|
|
|
WORKDIR /rails
|
|
|
|
|
|
|
|
|
|
ENV RAILS_ENV=production \
|
|
|
|
|
LANG=C.UTF-8 \
|
|
|
|
|
BUNDLE_JOBS=4 \
|
|
|
|
|
BUNDLE_RETRY=3 \
|
|
|
|
|
BUNDLE_PATH="/usr/local/bundle"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Throw-away build stage to reduce size of final image
|
|
|
|
|
FROM base as build
|
|
|
|
|
|
|
|
|
|
# Install packages needed to build gems
|
|
|
|
|
# This example intentionally does not require or install node.js
|
|
|
|
|
|
2022-04-24 22:52:31 -07:00
|
|
|
RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
|
|
|
|
|
build-essential \
|
|
|
|
|
gnupg2 \
|
2023-03-24 12:51:19 -07:00
|
|
|
libpq-dev \
|
2022-04-24 22:52:31 -07:00
|
|
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
|
|
|
|
|
|
|
|
|
RUN gem update --system && gem install bundler
|
|
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
# Install application gems
|
|
|
|
|
COPY Gemfile Gemfile.lock ./
|
2023-03-24 12:51:19 -07:00
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
# TODO: consolidate bundle config better, currently split between ENV and `bundle config`
|
2022-04-24 22:52:31 -07:00
|
|
|
RUN bundle config frozen true \
|
|
|
|
|
&& bundle config jobs 4 \
|
|
|
|
|
&& bundle config deployment true \
|
|
|
|
|
&& bundle config without 'development test' \
|
2023-03-24 11:28:28 -07:00
|
|
|
&& bundle install \
|
|
|
|
|
&& bundle exec bootsnap precompile --gemfile
|
2022-04-24 22:52:31 -07:00
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
# Copy application code
|
2022-04-24 22:52:31 -07:00
|
|
|
COPY . .
|
|
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
# Precompile bootsnap code for faster boot times
|
|
|
|
|
RUN bundle exec bootsnap precompile app/ lib/
|
|
|
|
|
|
2022-04-24 22:52:31 -07:00
|
|
|
# Precompile assets
|
|
|
|
|
# SECRET_KEY_BASE or RAILS_MASTER_KEY is required in production, but we don't
|
|
|
|
|
# want real secrets in the image or image history. The real secret is passed in
|
|
|
|
|
# at run time
|
|
|
|
|
ARG SECRET_KEY_BASE=fakekeyforassets
|
2023-03-24 12:47:07 -07:00
|
|
|
RUN ./bin/rails assets:precompile
|
|
|
|
|
|
|
|
|
|
# TODO: This will work in Rails 7.1
|
|
|
|
|
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
|
|
|
|
|
# RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
|
|
|
|
|
|
|
|
|
|
# Final stage for app image
|
|
|
|
|
FROM base
|
|
|
|
|
|
|
|
|
|
# Install packages needed for deployment
|
2023-03-24 13:00:09 -07:00
|
|
|
RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
|
|
|
|
|
libpq-dev \
|
|
|
|
|
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
2023-03-24 12:47:07 -07:00
|
|
|
|
|
|
|
|
# Copy built artifacts: gems, application
|
|
|
|
|
COPY --from=build /usr/local/bundle /usr/local/bundle
|
|
|
|
|
COPY --from=build /rails /rails
|
|
|
|
|
|
|
|
|
|
# Run and own only the runtime files as a non-root user for security
|
|
|
|
|
RUN useradd rails --home /rails --shell /bin/bash && \
|
|
|
|
|
chown -R rails:rails db log storage tmp
|
|
|
|
|
USER rails:rails
|
|
|
|
|
|
|
|
|
|
# TODO: migrate/consolidate to have all database migrations in here
|
|
|
|
|
# Entrypoint prepares the database.
|
|
|
|
|
# ENTRYPOINT ["/rails/bin/docker-entrypoint"]
|
2022-04-24 22:52:31 -07:00
|
|
|
|
2022-09-10 17:57:08 -07:00
|
|
|
# Run database migrations when deploying to Render. It is not great, maybe there's a better way?
|
2022-09-10 17:36:21 -07:00
|
|
|
# https://community.render.com/t/release-command-for-db-migrations/247/6
|
2022-09-10 17:57:08 -07:00
|
|
|
ARG RENDER
|
2022-09-10 17:30:26 -07:00
|
|
|
ARG DATABASE_URL
|
2023-03-25 10:09:34 -07:00
|
|
|
ARG SECRET_KEY_BASE
|
2022-09-10 17:53:39 -07:00
|
|
|
RUN if [ -z "$RENDER" ]; then echo "var is unset"; else bin/rails db:migrate; fi
|
2022-09-10 17:24:59 -07:00
|
|
|
|
2023-03-24 12:47:07 -07:00
|
|
|
# Start Server
|
2022-04-24 22:52:31 -07:00
|
|
|
EXPOSE 3000
|
|
|
|
|
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
|