Move production.Dockerfile closer to the rails template

This commit is contained in:
Ryan W
2023-09-30 11:20:46 -07:00
parent 1d8c3828a3
commit 4cf13fefd9
3 changed files with 25 additions and 33 deletions
+3 -3
View File
@@ -1,8 +1,8 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2 ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
FROM ruby:${RUBY_VERSION}-slim
# OS Level Dependencies # OS Level Dependencies
RUN --mount=type=cache,target=/var/cache/apt \ RUN --mount=type=cache,target=/var/cache/apt \
@@ -29,7 +29,7 @@ RUN gem update --system && gem install bundler
WORKDIR /usr/src/app WORKDIR /usr/src/app
ENTRYPOINT ["./bin/docker-entrypoint.sh"] ENTRYPOINT ["./bin/dev-docker-entrypoint"]
EXPOSE 3000 EXPOSE 3000
+22 -30
View File
@@ -1,23 +1,24 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2 ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
FROM ruby:${RUBY_VERSION}-slim as base
WORKDIR /rails WORKDIR /rails
# Set production environment
ENV RAILS_ENV=production \ ENV RAILS_ENV=production \
LANG=C.UTF-8 \ BUNDLE_DEPLOYMENT=1 \
BUNDLE_JOBS=4 \ BUNDLE_JOBS=4 \
BUNDLE_RETRY=3 \ BUNDLE_WITHOUT=development \
BUNDLE_PATH="/usr/local/bundle" BUNDLE_PATH=/usr/local/bundle
# Throw-away build stage to reduce size of final image # Throw-away build stage to reduce size of final image
FROM base as build FROM base as build
# Install packages needed to build gems # 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 \ RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt,sharing=locked \ --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 \ && apt-get install -yq --no-install-recommends \
build-essential \ build-essential \
gnupg2 \ gnupg2 \
less \
git \
libpq-dev \ libpq-dev \
git libvips \
pkg-config
RUN gem update --system && gem install bundler
# Install application gems # Install application gems
COPY Gemfile Gemfile.lock ./ COPY Gemfile Gemfile.lock ./
RUN bundle install && \
# TODO: consolidate bundle config better, currently split between ENV and `bundle config` rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
RUN bundle config frozen true \ bundle exec bootsnap precompile --gemfile
&& bundle config jobs 4 \
&& bundle config deployment true \
&& bundle config without 'development test' \
&& bundle install \
&& bundle exec bootsnap precompile --gemfile
# Copy application code # Copy application code
COPY . . COPY . .
@@ -53,6 +50,7 @@ RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image # Final stage for app image
FROM base 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; \ echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq \ apt-get update -qq \
&& apt-get install -yq --no-install-recommends \ && apt-get install -yq --no-install-recommends \
libpq-dev curl \
postgresql-client \
libvips
# Copy built artifacts: gems, application # Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security # 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 chown -R rails:rails db log storage tmp
USER rails:rails USER rails:rails
# TODO: migrate/consolidate to have all database migrations in here
# Entrypoint prepares the database. # 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? # Start the server by default, this can be overwritten at runtime
# 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
EXPOSE 3000 EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"] CMD ["./bin/rails", "server"]