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 practice .
6# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name practice practice
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
11ARG RUBY_VERSION=3.4.8
12FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
13
14# Rails app lives here
15WORKDIR /rails
16
17# Install base packages
18RUN apt-get update -qq && \
19 apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
20 ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \
21 rm -rf /var/lib/apt/lists /var/cache/apt/archives
22
23# Set production environment variables and enable jemalloc for reduced memory usage and latency.
24ENV RAILS_ENV="production" \
25 BUNDLE_DEPLOYMENT="1" \
26 BUNDLE_PATH="/usr/local/bundle" \
27 BUNDLE_WITHOUT="development" \
28 LD_PRELOAD="/usr/local/lib/libjemalloc.so"
29
30# Throw-away build stage to reduce size of final image
31FROM base AS build
32
33# Install packages needed to build gems
34RUN apt-get update -qq && \
35 apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
36 rm -rf /var/lib/apt/lists /var/cache/apt/archives
37
38# Install application gems
39COPY Gemfile Gemfile.lock vendor ./
40
41RUN bundle install && \
42 rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
43 # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
44 bundle exec bootsnap precompile -j 1 --gemfile
45
46# Copy application code
47COPY . .
48
49# Precompile bootsnap code for faster boot times.
50# -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
51RUN bundle exec bootsnap precompile -j 1 app/ lib/
52
53# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
54RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
55
56
57
58
59# Final stage for app image
60FROM base
61
62# Run and own only the runtime files as a non-root user for security
63RUN groupadd --system --gid 1000 rails && \
64 useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash
65USER 1000:1000
66
67# Copy built artifacts: gems, application
68COPY --chown=rails:rails --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
69COPY --chown=rails:rails --from=build /rails /rails
70
71# Entrypoint prepares the database.
72ENTRYPOINT ["/rails/bin/docker-entrypoint"]
73
74# Start server via Thruster by default, this can be overwritten at runtime
75EXPOSE 80
76CMD ["./bin/thrust", "./bin/rails", "server"]