1 # syntax=docker/dockerfile:1
4 # This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
5 # docker build -t oni_sorceress .
6 # docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name oni_sorceress oni_sorceress
8 # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
10 # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
11 ARG RUBY_VERSION=3.3.5
12 FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
14 # Rails app lives here
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
22 # Set production environment
23 ENV RAILS_ENV="production" \
24 BUNDLE_DEPLOYMENT="1" \
25 BUNDLE_PATH="/usr/local/bundle" \
26 BUNDLE_WITHOUT="development"
28 # Throw-away build stage to reduce size of final image
31 # Install packages needed to build gems and node modules
32 RUN apt-get update -qq && \
33 apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 && \
34 rm -rf /var/lib/apt/lists /var/cache/apt/archives
36 # Install JavaScript dependencies
38 ARG YARN_VERSION=latest
39 ENV PATH=/usr/local/node/bin:$PATH
40 RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
41 /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
42 npm install -g yarn@$YARN_VERSION && \
43 rm -rf /tmp/node-build-master
45 # Install application gems
46 COPY Gemfile Gemfile.lock ./
47 RUN bundle install && \
48 rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
49 bundle exec bootsnap precompile --gemfile
51 # Install node modules
52 COPY package.json yarn.lock ./
53 RUN yarn install --frozen-lockfile
55 # Copy application code
58 # Precompile bootsnap code for faster boot times
59 RUN bundle exec bootsnap precompile app/ lib/
61 # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
62 RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
65 RUN rm -rf node_modules
68 # Final stage for app image
71 # Copy built artifacts: gems, application
72 COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
73 COPY --from=build /rails /rails
75 # Run and own only the runtime files as a non-root user for security
76 RUN groupadd --system --gid 1000 rails && \
77 useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
78 chown -R rails:rails db log storage tmp
81 # Entrypoint prepares the database.
82 ENTRYPOINT ["/rails/bin/docker-entrypoint"]
84 # Start server via Thruster by default, this can be overwritten at runtime
86 CMD ["./bin/thrust", "./bin/rails", "server"]