Skip to content

Instantly share code, notes, and snippets.

@jmscarnatto
Last active January 25, 2025 15:10
Show Gist options
  • Save jmscarnatto/64af2821f5eb129927f5d37c01a5b3f5 to your computer and use it in GitHub Desktop.
Save jmscarnatto/64af2821f5eb129927f5d37c01a5b3f5 to your computer and use it in GitHub Desktop.
Rails Dockerfile - the definitive guide for production with precompile and migrations

Rails 'VERSION >=8.x' - SQLite ONLY

Dockerfile

ARG RUBY_VERSION=3.3.6

FROM ruby:$RUBY_VERSION

ENV RAILS_ENV=production \
    BUNDLE_PATH=/bundle \
    BUNDLE_WITHOUT="development test" \
    PATH=/app/bin:/app/vendor/bundle/bin:$PATH

WORKDIR /app

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    build-essential libpq-dev nodejs yarn && \
    rm -rf /var/lib/apt/lists/*

COPY Gemfile Gemfile.lock ./

RUN bundle install

COPY . .

RUN bundle exec rake assets:precompile

RUN bundle exec rake db:prepare

CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

docker-compose

services:
  app:
    container_name: <custom-container-name-app>
    build:
      context: .
      dockerfile: Dockerfile
    image: <custom-image-name>:latest
    command: bundle exec puma -C config/puma.rb
    ports:
      - "3000:3000"
    volumes:
      - db_data:/app/db

  jobs:
    container_name: <custom-container-name-jobs>
    build:
      context: .
      dockerfile: Dockerfile
    image: <custom-image-name>:latest
    command: bin/jobs
    volumes:
      - db_data:/app/db

volumes:
  db_data:

Rails 'VERSION >=6.x' - PostgreSQL + Redis

Dockerfile

ARG RUBY_VERSION=3.3.6

FROM ruby:$RUBY_VERSION as base

ENV RAILS_ENV=production \
    BUNDLE_PATH=/bundle \
    BUNDLE_WITHOUT="development test" \
    PATH=/app/bin:/app/vendor/bundle/bin:$PATH

WORKDIR /app

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    build-essential libpq-dev nodejs yarn && \
    rm -rf /var/lib/apt/lists/*

COPY Gemfile Gemfile.lock ./

RUN bundle install

COPY . .

# Assets precompile
FROM base as assets
RUN bundle exec rake assets:precompile

# Migrations
FROM base as migrations
RUN bundle exec rake db:migrate 2>/dev/null || bundle exec rake db:create db:migrate

# Application
FROM ruby:3.2 as application

WORKDIR /app

COPY --from=base /bundle /bundle
COPY --from=assets /app/public /app/public
COPY --from=base /app /app

ENV RAILS_ENV=production

CMD ["bundle", "exec", "sidekiq"]

docker-compose

version: '3.9'
services:
  app:
    build:
      context: .
      target: application
    command: bundle exec puma -C config/puma.rb
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/myappdb
      REDIS_URL: redis://redis:6379/0
    depends_on:
      - db
      - redis

  sidekiq:
    build:
      context: .
      target: application
    command: bundle exec sidekiq
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/myappdb
      REDIS_URL: redis://redis:6379/0
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: myappdb
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data

  redis:
    image: redis:7
    ports:
      - "6379:6379"

  migrate:
    build:
      context: .
      target: application
    command: bundle exec rails db:migrate
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/myappdb
    depends_on:
      - db

  assets:
    build:
      context: .
      target: assets
    # command: bundle exec rake assets:precompile
    volumes:
      - public:/app/public

volumes:
  db_data:
  public:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment