Skip to content

Instantly share code, notes, and snippets.

@marianposaceanu
Created March 23, 2026 08:17
Show Gist options
  • Select an option

  • Save marianposaceanu/a4be78582d9e0e6698f70ece225e0478 to your computer and use it in GitHub Desktop.

Select an option

Save marianposaceanu/a4be78582d9e0e6698f70ece225e0478 to your computer and use it in GitHub Desktop.
# syntax = docker/dockerfile:1
ARG RUBY_VERSION=4.0.1
FROM ruby:$RUBY_VERSION-slim AS base
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_WITHOUT="development:test" \
BUNDLE_DEPLOYMENT="1"
RUN gem update --system --no-document && \
gem install -N bundler
# Build stage
FROM base AS build
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
build-essential pkg-config libyaml-dev libsqlite3-dev && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy gem specs
COPY --link Gemfile Gemfile.lock ./
# Install gems including executables (puma)
ENV BUNDLE_PATH="/usr/local/bundle" \
PATH="/usr/local/bundle/bin:$PATH" \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
RUN bundle install && \
rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git
# Copy app code
COPY --link . .
# Make bin/* executable
RUN chmod +x bin/*
# Precompile assets (dummy secret to allow skipping credentials)
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Runtime stage
FROM base AS runtime
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
libsqlite3-0 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
ENV BUNDLE_PATH="/usr/local/bundle" \
PATH="/usr/local/bundle/bin:$PATH"
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
# Set up non-root user and permissions
RUN useradd rails --create-home --shell /bin/bash && \
mkdir -p /data /rails/db /rails/tmp /rails/log /rails/public && \
chown -R rails:rails /rails/db /rails/tmp /rails/log /rails/public /data
USER rails:rails
# Runtime ENV
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
RUBY_YJIT_ENABLE="1"
# Entrypoint prepares the DB
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
VOLUME /data
EXPOSE 80
# Start Puma with config file
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment