|
| 1 | +# syntax = docker/dockerfile:1 |
| 2 | + |
| 3 | +# Make sure it matches the Ruby version in .ruby-version and Gemfile |
| 4 | +ARG RUBY_VERSION=3.3.1 |
| 5 | +FROM ruby:$RUBY_VERSION-slim AS base |
| 6 | + |
| 7 | +# Rails app lives here |
| 8 | +WORKDIR /rails |
| 9 | + |
| 10 | +# Set production environment |
| 11 | +ENV RAILS_ENV="production" \ |
| 12 | + BUNDLE_DEPLOYMENT="1" \ |
| 13 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 14 | + BUNDLE_WITHOUT="development" |
| 15 | + |
| 16 | + |
| 17 | +# Throw-away build stage to reduce size of final image |
| 18 | +FROM base AS build |
| 19 | + |
| 20 | +# Install packages need to build gems |
| 21 | +RUN apt-get update -qq && \ |
| 22 | + apt-get install -y build-essential git pkg-config |
| 23 | + |
| 24 | +# Install application gems |
| 25 | +COPY Gemfile Gemfile.lock ./ |
| 26 | +RUN bundle install && \ |
| 27 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git |
| 28 | + |
| 29 | +# Copy application code |
| 30 | +COPY . . |
| 31 | + |
| 32 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 33 | +RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile |
| 34 | + |
| 35 | + |
| 36 | +# Final stage for app image |
| 37 | +FROM base |
| 38 | + |
| 39 | +# Configure environment defaults |
| 40 | +ENV HTTP_IDLE_TIMEOUT=60 |
| 41 | +ENV HTTP_READ_TIMEOUT=300 |
| 42 | +ENV HTTP_WRITE_TIMEOUT=300 |
| 43 | + |
| 44 | +# Install packages needed to run the application |
| 45 | +RUN apt-get update -qq && \ |
| 46 | + apt-get install --no-install-recommends -y libsqlite3-0 libvips curl ffmpeg redis && \ |
| 47 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 48 | + |
| 49 | +# Run and own the application files as a non-root user for security |
| 50 | +RUN useradd rails |
| 51 | +USER rails:rails |
| 52 | + |
| 53 | +# Copy built artifacts: gems, application |
| 54 | +COPY --from=build --chown=rails:rails /usr/local/bundle /usr/local/bundle |
| 55 | +COPY --from=build --chown=rails:rails /rails /rails |
| 56 | + |
| 57 | +# Set version and revision |
| 58 | +ARG APP_VERSION |
| 59 | +ENV APP_VERSION=$APP_VERSION |
| 60 | +ARG GIT_REVISION |
| 61 | +ENV GIT_REVISION=$GIT_REVISION |
| 62 | + |
| 63 | +# Expose ports for HTTP and HTTPS |
| 64 | +EXPOSE 80 443 |
| 65 | + |
| 66 | +# Start the server by default, this can be overwritten at runtime |
| 67 | +CMD ["bin/boot"] |
0 commit comments