This commit is contained in:
Christoph Haas 2026-02-08 23:07:07 +01:00
parent 4e23c6b4de
commit b25ef5882f
2 changed files with 34 additions and 45 deletions

View file

@ -1,98 +1,86 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
# check=error=true # check=error=true
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: # --- Version Arguments ---
# docker build -t example . ARG RUBY_VERSION=3.2.4
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name example example ARG NODE_VERSION=22.16.0
ARG YARN_VERSION=1.22.22
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
ARG COMMIT_SHA ARG COMMIT_SHA
ARG COMMIT_MESSAGE ARG COMMIT_MESSAGE
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version # --- Stage 1: Grab Node.js Binaries ---
ARG RUBY_VERSION=3.2.4 # We use the official image as a source for pre-compiled binaries
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base FROM docker.io/library/node:${NODE_VERSION}-slim AS node_source
# --- Stage 2: Base (Runtime Environment) ---
FROM docker.io/library/ruby:${RUBY_VERSION}-slim AS base
# Rails app lives here
WORKDIR /rails WORKDIR /rails
# Install base packages # Install runtime-only system packages
RUN apt-get update -qq && \ RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips libpq-dev nodejs telnet imagemagick && \ apt-get install --no-install-recommends -y \
curl libjemalloc2 libvips libpq-dev telnet imagemagick && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives rm -rf /var/lib/apt/lists /var/cache/apt/archives
# "Heist" Node.js and npm from the node_source stage
COPY --from=node_source /usr/local/bin/node /usr/local/bin/node
COPY --from=node_source /usr/local/lib/node_modules /usr/local/lib/node_modules
# Re-link npm and install Yarn (Instantly available)
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
npm install -g yarn@${YARN_VERSION}
# Set production environment # Set production environment
ENV RAILS_ENV="production" \ ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \ BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \ BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development" BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image # --- Stage 3: Build (Compilation & Assets) ---
FROM base AS build FROM base AS build
# Install packages needed to build gems and node modules # Install packages needed to build gems and node modules
RUN apt-get update -qq && \ RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libyaml-dev node-gyp pkg-config python-is-python3 && \ apt-get install --no-install-recommends -y \
build-essential git libyaml-dev node-gyp pkg-config python-is-python3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Install JavaScript dependencies # 1. Install Gems (Separate COPY for maximum caching)
ARG NODE_VERSION=22.16.0
ARG YARN_VERSION=1.22.22
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Install application gems
COPY Gemfile Gemfile.lock ./ COPY Gemfile Gemfile.lock ./
RUN bundle install && \ RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile bundle exec bootsnap precompile --gemfile
# Install node modules # 2. Install JavaScript dependencies (Separate COPY for maximum caching)
COPY package.json yarn.lock ./ COPY package.json yarn.lock ./
RUN yarn install --immutable RUN yarn install --immutable
# Copy application code # 3. Copy application code
COPY . . COPY . .
# Precompile bootsnap code for faster boot times # 4. Precompile assets and bootsnap
RUN bundle exec bootsnap precompile app/ lib/ RUN bundle exec bootsnap precompile app/ lib/ && \
SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \
rm -rf node_modules tmp/cache
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY # --- Stage 4: Final Production Image ---
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
RUN rm -rf node_modules
# Final stage for app image
FROM base FROM base
ARG COMMIT_SHA ARG COMMIT_SHA
ARG COMMIT_MESSAGE ARG COMMIT_MESSAGE
# Copy built artifacts: gems, application # Copy built artifacts: gems and the app (with precompiled assets)
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails COPY --from=build /rails /rails
# Add build information so we can check if the deployment works # Security: Run as non-root user
RUN echo "Commit SHA: ${COMMIT_SHA}" > /rails/public/versioninfo && \
echo "Commit message: ${COMMIT_MESSAGE}" >> /rails/public/versioninfo
RUN echo "versioninfo: SHA is [${COMMIT_SHA}] and DATE is [${BUILD_DATE}]"
# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \ RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log tmp public chown -R rails:rails db log tmp public
USER 1000:1000 USER 1000:1000
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"] ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80 EXPOSE 80
CMD ["./bin/thrust", "./bin/rails", "server"] CMD ["./bin/thrust", "./bin/rails", "server"]

1
public/versioninfo Normal file
View file

@ -0,0 +1 @@
So 8. Feb 22:59:33 CET 2026