# Ruby on Rails Production Stack

> Production Rails with Puma, Sidekiq, PostgreSQL, and Redis.

- Category: Full Web Stacks
- Difficulty: advanced
- Components: rails, puma, sidekiq, postgresql, redis, nginx
- Tags: rails, ruby, puma, sidekiq, production
- Source: https://docker.recipes/full-stacks/rails-production

## Overview

Ruby on Rails is a full-stack web application framework built on Ruby that follows convention-over-configuration principles, enabling rapid development of database-backed web applications. Created by David Heinemeier Hansson in 2004, Rails popularized the Model-View-Controller (MVC) architecture and RESTful design patterns, making it the backbone for companies like GitHub, Shopify, and Basecamp. This production stack combines Rails with Puma as the multi-threaded web server, Sidekiq for background job processing, PostgreSQL for robust data persistence, Redis for caching and job queues, and Nginx for reverse proxy and static file serving. The architecture addresses critical production concerns including horizontal scaling through Puma's thread-based concurrency, asynchronous job processing via Sidekiq, high-performance caching with Redis, and enterprise-grade data integrity through PostgreSQL's ACID compliance. This configuration is ideal for growing startups transitioning from development to production, established companies running Rails applications at scale, and teams requiring a battle-tested stack that can handle complex business logic, background processing, and high-traffic scenarios while maintaining Rails' developer productivity advantages.

## Features

- Puma multi-threaded web server with configurable worker processes and thread pools for concurrent request handling
- Sidekiq background job processing with Redis-backed queues supporting retry logic and job scheduling
- PostgreSQL ACID-compliant database with JSON/JSONB support for mixed relational and document data models
- Redis in-memory data store providing sub-millisecond response times for session storage and caching layers
- Nginx reverse proxy with SSL/TLS termination, static file serving, and load balancing capabilities
- Rails Active Storage integration for file uploads with configurable storage backends
- Production-optimized environment variables including SECRET_KEY_BASE and database connection pooling
- Containerized asset pipeline with precompiled assets for optimized static resource delivery

## Use cases

- E-commerce platforms requiring background order processing, inventory management, and payment handling
- Content management systems with file uploads, caching, and complex user permission hierarchies
- SaaS applications needing multi-tenancy, scheduled jobs, and real-time notifications
- API backends for mobile applications with database-heavy operations and background sync processes
- Enterprise web applications requiring audit trails, complex reporting, and data integrity guarantees
- Startup MVPs transitioning from development to production with scalable architecture patterns
- Legacy Rails applications being containerized for cloud deployment and DevOps automation

## Prerequisites

- Minimum 2GB RAM (1GB+ for PostgreSQL, 512MB+ for Redis, 512MB+ for Rails/Puma processes)
- Ruby application with Gemfile containing rails, puma, sidekiq, and pg gems
- Dockerfile configured for Rails production environment with asset precompilation
- Environment variables: POSTGRES_PASSWORD, SECRET_KEY_BASE for secure deployment
- Nginx configuration file (nginx.conf) with upstream Rails server definitions
- Rails database migrations and schema ready for production deployment

## docker-compose.yml

```yaml
services:
  postgres:
    image: postgres:15-alpine
    environment:
      - POSTGRES_USER=rails
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=rails_production
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - rails_net

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    networks:
      - rails_net

  rails:
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec puma -C config/puma.rb
    environment:
      - DATABASE_URL=postgres://rails:${POSTGRES_PASSWORD}@postgres:5432/rails_production
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY_BASE=${SECRET_KEY_BASE}
      - RAILS_ENV=production
      - RAILS_SERVE_STATIC_FILES=true
    volumes:
      - rails_storage:/app/storage
    depends_on:
      - postgres
      - redis
    networks:
      - rails_net

  sidekiq:
    build:
      context: .
      dockerfile: Dockerfile
    command: bundle exec sidekiq
    environment:
      - DATABASE_URL=postgres://rails:${POSTGRES_PASSWORD}@postgres:5432/rails_production
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY_BASE=${SECRET_KEY_BASE}
      - RAILS_ENV=production
    depends_on:
      - postgres
      - redis
    networks:
      - rails_net

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - rails
    networks:
      - rails_net

volumes:
  postgres_data:
  redis_data:
  rails_storage:

networks:
  rails_net:
```

## .env

```bash
# Rails Production
POSTGRES_PASSWORD=secure_postgres_password
SECRET_KEY_BASE=your_very_long_secret_key_base_here

# Rails at http://localhost
```

## Usage notes

- Rails at http://localhost
- Run db:migrate before start
- Precompile assets
- Sidekiq for background jobs
- Active Storage configured

## Troubleshooting

- ActiveRecord::ConnectionNotEstablished: Verify DATABASE_URL format and ensure PostgreSQL container is running before Rails startup
- Redis::CannotConnectError in Sidekiq: Check REDIS_URL environment variable and confirm Redis container accessibility on port 6379
- Puma workers timing out under load: Adjust thread pool settings in config/puma.rb and increase container memory allocation
- Rails asset compilation failures: Ensure RAILS_ENV=production and SECRET_KEY_BASE are set during Docker build process
- 502 Bad Gateway from Nginx: Verify Rails application is binding to 0.0.0.0 rather than localhost in Puma configuration
- Sidekiq jobs not processing: Confirm background job classes are loaded and Redis connection pool size accommodates worker threads

