Move production.Dockerfile closer to the rails template
This commit is contained in:
+22
-30
@@ -1,23 +1,24 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
|
||||
ARG RUBY_VERSION=3.2.2
|
||||
|
||||
FROM ruby:${RUBY_VERSION}-slim as base
|
||||
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
|
||||
|
||||
WORKDIR /rails
|
||||
|
||||
# Set production environment
|
||||
ENV RAILS_ENV=production \
|
||||
LANG=C.UTF-8 \
|
||||
BUNDLE_DEPLOYMENT=1 \
|
||||
BUNDLE_JOBS=4 \
|
||||
BUNDLE_RETRY=3 \
|
||||
BUNDLE_PATH="/usr/local/bundle"
|
||||
BUNDLE_WITHOUT=development \
|
||||
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
|
||||
# NOTE: This example project intentionally does not require or install node.js
|
||||
|
||||
RUN --mount=type=cache,target=/var/cache/apt \
|
||||
--mount=type=cache,target=/var/lib/apt,sharing=locked \
|
||||
@@ -28,21 +29,17 @@ RUN --mount=type=cache,target=/var/cache/apt \
|
||||
&& apt-get install -yq --no-install-recommends \
|
||||
build-essential \
|
||||
gnupg2 \
|
||||
less \
|
||||
git \
|
||||
libpq-dev \
|
||||
git
|
||||
|
||||
RUN gem update --system && gem install bundler
|
||||
libvips \
|
||||
pkg-config
|
||||
|
||||
# Install application gems
|
||||
COPY Gemfile Gemfile.lock ./
|
||||
|
||||
# TODO: consolidate bundle config better, currently split between ENV and `bundle config`
|
||||
RUN bundle config frozen true \
|
||||
&& bundle config jobs 4 \
|
||||
&& bundle config deployment true \
|
||||
&& bundle config without 'development test' \
|
||||
&& bundle install \
|
||||
&& bundle exec bootsnap precompile --gemfile
|
||||
RUN bundle install && \
|
||||
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
|
||||
bundle exec bootsnap precompile --gemfile
|
||||
|
||||
# Copy application code
|
||||
COPY . .
|
||||
@@ -53,6 +50,7 @@ RUN bundle exec bootsnap precompile app/ lib/
|
||||
# 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
|
||||
|
||||
@@ -64,28 +62,22 @@ RUN --mount=type=cache,target=/var/cache/apt \
|
||||
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
|
||||
apt-get update -qq \
|
||||
&& apt-get install -yq --no-install-recommends \
|
||||
libpq-dev
|
||||
curl \
|
||||
postgresql-client \
|
||||
libvips
|
||||
|
||||
# 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 && \
|
||||
RUN useradd rails --create-home --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"]
|
||||
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
|
||||
|
||||
# Run database migrations when deploying to Render. It is not great, maybe there's a better way?
|
||||
# https://community.render.com/t/release-command-for-db-migrations/247/6
|
||||
ARG RENDER
|
||||
ARG DATABASE_URL
|
||||
ARG SECRET_KEY_BASE
|
||||
RUN if [ -z "$RENDER" ]; then echo "var is unset"; else bin/rails db:migrate; fi
|
||||
|
||||
# Start Server
|
||||
# Start the server by default, this can be overwritten at runtime
|
||||
EXPOSE 3000
|
||||
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
|
||||
CMD ["./bin/rails", "server"]
|
||||
|
||||
Reference in New Issue
Block a user