|
| 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 demo . |
| 6 | +# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name demo demo |
| 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=xxx |
| 12 | +FROM ruby:$RUBY_VERSION-alpine AS base |
| 13 | + |
| 14 | +# Rails app lives here |
| 15 | +WORKDIR /rails |
| 16 | + |
| 17 | +# Update gems and bundler |
| 18 | +RUN gem update --system --no-document && \ |
| 19 | + gem install -N bundler |
| 20 | + |
| 21 | +# Install base packages |
| 22 | +RUN apk add --no-cache curl jemalloc sqlite tzdata |
| 23 | + |
| 24 | +# Set production environment |
| 25 | +ENV BUNDLE_DEPLOYMENT="1" \ |
| 26 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 27 | + BUNDLE_WITHOUT="development:test" \ |
| 28 | + RAILS_ENV="production" |
| 29 | + |
| 30 | + |
| 31 | +# Throw-away build stage to reduce size of final image |
| 32 | +FROM base AS build |
| 33 | + |
| 34 | +# Install packages needed to build gems |
| 35 | +RUN apk add --no-cache build-base pkgconfig unzip yaml-dev |
| 36 | + |
| 37 | +ARG BUN_VERSION=xxx |
| 38 | +FROM oven/bun:${BUN_VERSION}-alpine AS bun |
| 39 | + |
| 40 | +# Copy Bun from official image |
| 41 | +COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun |
| 42 | +ENV PATH=/usr/local/bin:$PATH |
| 43 | + |
| 44 | +# Install application gems |
| 45 | +COPY Gemfile Gemfile.lock ./ |
| 46 | +RUN bundle install && \ |
| 47 | + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ |
| 48 | + bundle exec bootsnap precompile --gemfile |
| 49 | + |
| 50 | +# Install node modules |
| 51 | +COPY package.json bun.lock ./ |
| 52 | +RUN bun install --frozen-lockfile --production |
| 53 | + |
| 54 | +# Copy application code |
| 55 | +COPY . . |
| 56 | + |
| 57 | +# Precompile bootsnap code for faster boot times |
| 58 | +RUN bundle exec bootsnap precompile app/ lib/ |
| 59 | + |
| 60 | +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY |
| 61 | +RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile |
| 62 | + |
| 63 | + |
| 64 | +# Final stage for app image |
| 65 | +FROM base |
| 66 | + |
| 67 | +# Install packages needed for deployment |
| 68 | +RUN apk add --no-cache sqlite-libs |
| 69 | + |
| 70 | +# Copy built artifacts: gems, application |
| 71 | +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" |
| 72 | +COPY --from=build /rails /rails |
| 73 | + |
| 74 | +# Run and own only the runtime files as a non-root user for security |
| 75 | +RUN addgroup --system --gid 1000 rails && \ |
| 76 | + adduser --system rails --uid 1000 --ingroup rails --home /home/rails --shell /bin/sh rails && \ |
| 77 | + mkdir /data && \ |
| 78 | + chown -R 1000:1000 db log storage tmp /data |
| 79 | +USER 1000:1000 |
| 80 | + |
| 81 | +# Deployment options |
| 82 | +ENV DATABASE_URL="sqlite3:///data/production.sqlite3" |
| 83 | + |
| 84 | +# Entrypoint prepares the database. |
| 85 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 86 | + |
| 87 | +# Start server via Thruster by default, this can be overwritten at runtime |
| 88 | +EXPOSE 80 |
| 89 | +VOLUME /data |
| 90 | +CMD ["./bin/thrust", "./bin/rails", "server"] |
0 commit comments