|
| 1 | +# syntax = docker/dockerfile:1 |
| 2 | + |
| 3 | +# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: |
| 4 | +# docker build -t my-app . |
| 5 | +# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app |
| 6 | + |
| 7 | +# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html |
| 8 | + |
| 9 | +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version |
| 10 | +ARG RUBY_VERSION=3.3.3 |
| 11 | +FROM docker.io/library/ruby:$RUBY_VERSION-slim as base |
| 12 | + |
| 13 | +# Rails app lives here |
| 14 | +WORKDIR /rails |
| 15 | + |
| 16 | +# Install base packages |
| 17 | +RUN apt-get update -qq && \ |
| 18 | + apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 && \ |
| 19 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 20 | + |
| 21 | +# Set production environment |
| 22 | +ENV RAILS_ENV="production" \ |
| 23 | + BUNDLE_DEPLOYMENT="1" \ |
| 24 | + BUNDLE_PATH="/usr/local/bundle" \ |
| 25 | + BUNDLE_WITHOUT="development" |
| 26 | + |
| 27 | +# Throw-away build stage to reduce size of final image |
| 28 | +FROM base as build |
| 29 | + |
| 30 | +# Install packages needed to build gems and node modules |
| 31 | +RUN apt-get update -qq && \ |
| 32 | + apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 && \ |
| 33 | + rm -rf /var/lib/apt/lists /var/cache/apt/archives |
| 34 | + |
| 35 | +# Install JavaScript dependencies |
| 36 | +ARG NODE_VERSION=18.19.0 |
| 37 | +ARG YARN_VERSION=1.22.19 |
| 38 | +ENV PATH=/usr/local/node/bin:$PATH |
| 39 | +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ |
| 40 | + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ |
| 41 | + npm install -g yarn@$YARN_VERSION && \ |
| 42 | + rm -rf /tmp/node-build-master |
| 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 yarn.lock ./ |
| 52 | +RUN yarn install --frozen-lockfile |
| 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 | +RUN rm -rf node_modules |
| 65 | + |
| 66 | + |
| 67 | +# Final stage for app image |
| 68 | +FROM base |
| 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 groupadd --system --gid 1000 rails && \ |
| 76 | + useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ |
| 77 | + chown -R rails:rails db log storage tmp |
| 78 | +USER 1000:1000 |
| 79 | + |
| 80 | +# Entrypoint prepares the database. |
| 81 | +ENTRYPOINT ["/rails/bin/docker-entrypoint"] |
| 82 | + |
| 83 | +# Start the server by default, this can be overwritten at runtime |
| 84 | +EXPOSE 3000 |
| 85 | +CMD ["./bin/rails", "server"] |
0 commit comments