|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +# check=error=true |
| 3 | + |
| 4 | +# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: |
| 5 | +# docker build -t sample_app . |
| 6 | +# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name sample_app sample_app |
| 7 | + |
| 8 | +# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html |
| 9 | + |
| 10 | +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version |
| 11 | +ARG RUBY_VERSION=3.4.3 |
| 12 | +FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base |
| 13 | + |
| 14 | +# Rails app lives here |
| 15 | +WORKDIR /rails |
| 16 | + |
| 17 | +# Install base packages |
| 18 | +RUN apt-get update -qq && \ |
| 19 | + apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \ |
| 20 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 21 | + |
| 22 | +# Set production environment |
| 23 | +ENV RAILS_ENV="production" \ |
| 24 | + BUNDLE_DEPLOYMENT="1" \ |
| 25 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 26 | + BUNDLE_WITHOUT="development" |
| 27 | + |
| 28 | +# Throw-away build stage to reduce size of final image |
| 29 | +FROM base AS build |
| 30 | + |
| 31 | +# Install packages needed to build gems |
| 32 | +RUN apt-get update -qq && \ |
| 33 | + apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \ |
| 34 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 35 | + |
| 36 | +# Install application gems |
| 37 | +COPY Gemfile Gemfile.lock ./ |
| 38 | +RUN bundle install && \ |
| 39 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ |
| 40 | + bundle exec bootsnap precompile --gemfile |
| 41 | + |
| 42 | +# Copy application code |
| 43 | +COPY . . |
| 44 | + |
| 45 | +# Precompile bootsnap code for faster boot times |
| 46 | +RUN bundle exec bootsnap precompile app/ lib/ |
| 47 | + |
| 48 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 49 | +RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +# Final stage for app image |
| 55 | +FROM base |
| 56 | + |
| 57 | +# Copy built artifacts: gems, application |
| 58 | +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" |
| 59 | +COPY --from=build /rails /rails |
| 60 | + |
| 61 | +# Run and own only the runtime files as a non-root user for security |
| 62 | +RUN groupadd --system --gid 1000 rails && \ |
| 63 | + useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ |
| 64 | + chown -R rails:rails db log storage tmp |
| 65 | +USER 1000:1000 |
| 66 | + |
| 67 | +# Entrypoint prepares the database. |
| 68 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 69 | + |
| 70 | +# Start server via Thruster by default, this can be overwritten at runtime |
| 71 | +EXPOSE 80 |
| 72 | +CMD ["./bin/thrust", "./bin/rails", "server"] |
0 commit comments