# syntax=docker/dockerfile:1 # check=error=true # --- Version Arguments --- ARG RUBY_VERSION=3.2.4 ARG NODE_VERSION=22.16.0 ARG YARN_VERSION=1.22.22 # --- Stage 1: Grab Node.js Binaries --- # We use the official image as a source for pre-compiled binaries 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 WORKDIR /rails # Install runtime-only system packages RUN apt-get update -qq && \ apt-get install --no-install-recommends -y \ curl libjemalloc2 libvips libpq-dev telnet imagemagick && \ 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 ENV RAILS_ENV="production" \ BUNDLE_DEPLOYMENT="1" \ BUNDLE_PATH="/usr/local/bundle" \ BUNDLE_WITHOUT="development" # --- Stage 3: Build (Compilation & Assets) --- FROM base AS build # Install packages needed to build gems and node modules RUN apt-get update -qq && \ 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 # 1. Install Gems (Separate COPY for maximum caching) COPY Gemfile Gemfile.lock ./ RUN bundle install && \ rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ bundle exec bootsnap precompile --gemfile # 2. Install JavaScript dependencies (Separate COPY for maximum caching) COPY package.json yarn.lock ./ RUN yarn install --immutable # 3. Copy application code COPY . . # 4. Precompile assets and bootsnap RUN bundle exec bootsnap precompile app/ lib/ && \ SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \ rm -rf node_modules tmp/cache # --- Stage 4: Final Production Image --- FROM base # Copy built artifacts: gems and the app (with precompiled assets) COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" COPY --from=build /rails /rails # Security: Run as non-root user RUN groupadd --system --gid 1000 rails && \ useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ chown -R rails:rails db log tmp public USER 1000:1000 ENTRYPOINT ["/rails/bin/docker-entrypoint"] EXPOSE 80 CMD ["./bin/thrust", "./bin/rails", "server"]