|
| 1 | +# syntax = docker/dockerfile:1 |
| 2 | + |
| 3 | +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile |
| 4 | +ARG RUBY_VERSION=3.3.3 |
| 5 | +FROM ruby:$RUBY_VERSION-slim as base |
| 6 | + |
| 7 | +LABEL fly_launch_runtime="rails" |
| 8 | + |
| 9 | +# Rails app lives here |
| 10 | +WORKDIR /rails |
| 11 | + |
| 12 | +# Set production environment |
| 13 | +ENV BUNDLE_DEPLOYMENT="1" \ |
| 14 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 15 | + BUNDLE_WITHOUT="development:test" \ |
| 16 | + RAILS_ENV="production" |
| 17 | + |
| 18 | +# Update gems and bundler |
| 19 | +RUN gem update --system --no-document && \ |
| 20 | + gem install -N bundler |
| 21 | + |
| 22 | + |
| 23 | +# Throw-away build stage to reduce size of final image |
| 24 | +FROM base as build |
| 25 | + |
| 26 | +# Install packages needed to build gems and node modules |
| 27 | +RUN apt-get update -qq && \ |
| 28 | + apt-get install --no-install-recommends -y build-essential curl libpq-dev node-gyp pkg-config python-is-python3 |
| 29 | + |
| 30 | +# Install Node.js |
| 31 | +ARG NODE_VERSION=20.16.0 |
| 32 | +ENV PATH=/usr/local/node/bin:$PATH |
| 33 | +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ |
| 34 | + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ |
| 35 | + rm -rf /tmp/node-build-master |
| 36 | + |
| 37 | +# Install application gems |
| 38 | +COPY --link Gemfile Gemfile.lock ./ |
| 39 | +RUN bundle install && \ |
| 40 | + bundle exec bootsnap precompile --gemfile && \ |
| 41 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git |
| 42 | + |
| 43 | +# Install node modules |
| 44 | +COPY --link package.json package-lock.json ./ |
| 45 | +RUN npm install |
| 46 | + |
| 47 | +# Copy application code |
| 48 | +COPY --link . . |
| 49 | + |
| 50 | +# Precompile bootsnap code for faster boot times |
| 51 | +RUN bundle exec bootsnap precompile app/ lib/ |
| 52 | + |
| 53 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 54 | +RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile |
| 55 | + |
| 56 | + |
| 57 | +# Final stage for app image |
| 58 | +FROM base |
| 59 | + |
| 60 | +# Install packages needed for deployment |
| 61 | +RUN apt-get update -qq && \ |
| 62 | + apt-get install --no-install-recommends -y curl postgresql-client && \ |
| 63 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 64 | + |
| 65 | +# Copy built artifacts: gems, application |
| 66 | +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" |
| 67 | +COPY --from=build /rails /rails |
| 68 | + |
| 69 | +# Run and own only the runtime files as a non-root user for security |
| 70 | +RUN groupadd --system --gid 1000 rails && \ |
| 71 | + useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ |
| 72 | + chown -R 1000:1000 db log storage tmp |
| 73 | +USER 1000:1000 |
| 74 | + |
| 75 | +# Deployment options |
| 76 | +ENV RAILS_LOG_TO_STDOUT="1" \ |
| 77 | + RAILS_SERVE_STATIC_FILES="true" |
| 78 | + |
| 79 | +# Entrypoint sets up the container. |
| 80 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 81 | + |
| 82 | +# Start the server by default, this can be overwritten at runtime |
| 83 | +EXPOSE 3000 |
| 84 | +CMD ["./bin/rails", "server"] |
0 commit comments