11# syntax = docker/dockerfile:1
22
3- # Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
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
410ARG RUBY_VERSION=your-ruby-version
5- FROM registry. docker.com /library/ruby:$RUBY_VERSION-slim as base
11+ FROM docker.io /library/ruby:$RUBY_VERSION-slim as base
612
713# Rails app lives here
814WORKDIR /rails
915
16+ # Install base packages
17+ RUN apt-get update -qq && \
18+ apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libvips && \
19+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
20+
1021# Set production environment
1122ENV RAILS_ENV="production" \
1223 BUNDLE_DEPLOYMENT="1" \
1324 BUNDLE_PATH="/usr/local/bundle" \
1425 BUNDLE_WITHOUT="development"
1526
16-
1727# Throw-away build stage to reduce size of final image
1828FROM base as build
1929
2030# Install packages needed to build gems
2131RUN apt-get update -qq && \
22- apt-get install --no-install-recommends -y build-essential git libvips pkg-config
32+ apt-get install --no-install-recommends -y build-essential git pkg-config && \
33+ rm -rf /var/lib/apt/lists /var/cache/apt/archives
2334
2435# Install application gems
2536COPY Gemfile Gemfile.lock ./
@@ -37,22 +48,20 @@ RUN bundle exec bootsnap precompile app/ lib/
3748RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
3849
3950
51+
52+
4053# Final stage for app image
4154FROM base
4255
43- # Install packages needed for deployment
44- RUN apt-get update -qq && \
45- apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
46- rm -rf /var/lib/apt/lists /var/cache/apt/archives
47-
4856# Copy built artifacts: gems, application
49- COPY --from=build /usr/local/bundle /usr/local/bundle
57+ COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
5058COPY --from=build /rails /rails
5159
5260# Run and own only the runtime files as a non-root user for security
53- RUN useradd rails --create-home --shell /bin/bash && \
61+ RUN groupadd --system --gid 1000 rails && \
62+ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
5463 chown -R rails:rails db log storage tmp
55- USER rails:rails
64+ USER 1000:1000
5665
5766# Entrypoint prepares the database.
5867ENTRYPOINT ["/rails/bin/docker-entrypoint" ]
0 commit comments