Skip to content

Instantly share code, notes, and snippets.

@nicklozon
Created December 10, 2024 15:05
Show Gist options
  • Save nicklozon/562401734e8d323ad69d8a61e9314e97 to your computer and use it in GitHub Desktop.
Save nicklozon/562401734e8d323ad69d8a61e9314e97 to your computer and use it in GitHub Desktop.
# syntax = docker/dockerfile:1
ARG RUBY_VERSION=3.3.4
ARG NODE_VERSION=22.8.0
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="development"
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git libpq-dev
# Node & Yarn
ARG NODE_VERSION
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
RUN apt-get install -y nodejs=$NODE_VERSION-1nodesource1
RUN corepack enable
# Throw-away build stage to reduce size of final image
FROM base as build
# Install node dependencies
COPY package.json yarn.lock .yarnrc.yml ./
RUN yarn install --immutable
# Install packages needed to build gems
RUN apt-get install --no-install-recommends -y pkg-config
# Install application gems
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
# Copy application code
COPY . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
# Clean-up aptitude
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash
RUN chown -R rails:rails /rails /usr/local/bundle
USER rails:rails
# Use corepack as rails user
RUN corepack use yarn
VOLUME /usr/local/bundle/cache
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/dev"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment