Ruby on Rails + PostgreSQL
Ruby on Rails with PostgreSQL and Redis for background jobs.
Overview
Ruby on Rails is a full-stack web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. Created by David Heinemeier Hansson in 2003, Rails emphasizes convention over configuration and the DRY (Don't Repeat Yourself) principle, enabling rapid development of database-backed web applications. Rails includes everything needed to create database-backed web applications according to the MVC pattern, with built-in support for routing, templating, Active Record ORM, and Action Mailer. This stack combines Rails with PostgreSQL as the primary database and Redis for session storage, caching, and background job processing through Sidekiq. PostgreSQL provides ACID-compliant relational data storage with advanced features like JSON columns, full-text search, and complex query capabilities that complement Rails' Active Record ORM. Redis serves dual purposes: acting as a high-performance cache to reduce database load and powering Sidekiq for asynchronous job processing, which is essential for handling tasks like email delivery, file processing, and API integrations without blocking web requests. This combination is ideal for startups building MVPs, established companies running production web applications, and development teams who need a proven stack that scales from prototype to enterprise. Rails' rapid development capabilities combined with PostgreSQL's reliability and Redis' performance create a robust foundation for modern web applications that need both developer productivity and production stability.
Key Features
- Active Record ORM with PostgreSQL-specific features including JSON/JSONB column support and advanced query capabilities
- Sidekiq background job processing powered by Redis for handling asynchronous tasks like email delivery and file processing
- Rails Action Cable WebSocket support using Redis as the adapter for real-time features
- PostgreSQL full-text search integration with Rails' built-in search functionality
- Redis-backed Rails caching for fragment caching, page caching, and Active Support::Cache
- Rails database migrations with PostgreSQL-specific features like partial indexes and custom data types
- Action Mailer integration with Sidekiq for asynchronous email delivery
- Rails session storage in Redis for improved performance and horizontal scaling
Common Use Cases
- 1E-commerce platforms requiring complex product catalogs, inventory management, and background order processing
- 2Content management systems needing full-text search capabilities and real-time collaboration features
- 3SaaS applications with multi-tenant architecture using PostgreSQL's row-level security
- 4Social media platforms requiring real-time notifications, feed generation, and image processing jobs
- 5Financial applications needing ACID compliance for transactions and audit trails
- 6API-first applications serving mobile apps with Rails API mode and background data synchronization
- 7Enterprise web applications requiring complex reporting, data analytics, and scheduled job processing
Prerequisites
- Docker and Docker Compose installed with at least 2GB available RAM for all services
- Port 3000 available for Rails web server access
- Basic knowledge of Ruby syntax and Rails conventions (MVC pattern, Active Record)
- Understanding of relational database concepts for PostgreSQL schema design
- Familiarity with environment variables for database credentials and Redis configuration
- Git repository with Gemfile including rails, pg, redis, and sidekiq gems
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 web: 3 build: .4 container_name: rails5 command: bundle exec rails server -b 0.0.0.06 volumes: 7 - .:/app8 environment: 9 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}10 REDIS_URL: redis://redis:6379/011 ports: 12 - "3000:3000"13 depends_on: 14 - postgres15 - redis16 networks: 17 - rails1819 postgres: 20 image: postgres:16-alpine21 container_name: postgres22 environment: 23 POSTGRES_DB: ${DB_NAME}24 POSTGRES_USER: ${DB_USER}25 POSTGRES_PASSWORD: ${DB_PASSWORD}26 volumes: 27 - postgres_data:/var/lib/postgresql/data28 networks: 29 - rails3031 redis: 32 image: redis:alpine33 container_name: redis34 networks: 35 - rails3637 sidekiq: 38 build: .39 command: bundle exec sidekiq40 environment: 41 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}42 REDIS_URL: redis://redis:6379/043 depends_on: 44 - postgres45 - redis46 networks: 47 - rails4849volumes: 50 postgres_data: 5152networks: 53 rails: 54 driver: bridge.env Template
.env
1DB_NAME=rails2DB_USER=rails3DB_PASSWORD=changemeUsage Notes
- 1Docs: https://guides.rubyonrails.org/
- 2Create Dockerfile with Ruby + bundler
- 3Run migrations: docker compose exec web rails db:migrate
- 4Access at http://localhost:3000
- 5Rails console: docker compose exec web rails c
- 6Sidekiq for background jobs (Action Mailer, Active Job)
Individual Services(4 services)
Copy individual services to mix and match with your existing compose files.
web
web:
build: .
container_name: rails
command: bundle exec rails server -b 0.0.0.0
volumes:
- .:/app
environment:
DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
REDIS_URL: redis://redis:6379/0
ports:
- "3000:3000"
depends_on:
- postgres
- redis
networks:
- rails
postgres
postgres:
image: postgres:16-alpine
container_name: postgres
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- rails
redis
redis:
image: redis:alpine
container_name: redis
networks:
- rails
sidekiq
sidekiq:
build: .
command: bundle exec sidekiq
environment:
DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
REDIS_URL: redis://redis:6379/0
depends_on:
- postgres
- redis
networks:
- rails
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 web:5 build: .6 container_name: rails7 command: bundle exec rails server -b 0.0.0.08 volumes:9 - .:/app10 environment:11 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}12 REDIS_URL: redis://redis:6379/013 ports:14 - "3000:3000"15 depends_on:16 - postgres17 - redis18 networks:19 - rails2021 postgres:22 image: postgres:16-alpine23 container_name: postgres24 environment:25 POSTGRES_DB: ${DB_NAME}26 POSTGRES_USER: ${DB_USER}27 POSTGRES_PASSWORD: ${DB_PASSWORD}28 volumes:29 - postgres_data:/var/lib/postgresql/data30 networks:31 - rails3233 redis:34 image: redis:alpine35 container_name: redis36 networks:37 - rails3839 sidekiq:40 build: .41 command: bundle exec sidekiq42 environment:43 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}44 REDIS_URL: redis://redis:6379/045 depends_on:46 - postgres47 - redis48 networks:49 - rails5051volumes:52 postgres_data:5354networks:55 rails:56 driver: bridge57EOF5859# 2. Create the .env file60cat > .env << 'EOF'61DB_NAME=rails62DB_USER=rails63DB_PASSWORD=changeme64EOF6566# 3. Start the services67docker compose up -d6869# 4. View logs70docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/rails-postgres/run | bashTroubleshooting
- Gem::Ext::BuildError during bundle install: Install build-essential and libpq-dev in Dockerfile before running bundle install
- PG::ConnectionBad - could not connect to server: Ensure postgres service starts before web service using depends_on and verify DATABASE_URL format
- Redis::CannotConnectError in Sidekiq: Check REDIS_URL environment variable format and ensure redis service is running before sidekiq
- ActiveRecord::PendingMigrationError: Run 'docker compose exec web rails db:create db:migrate' to set up database schema
- Sidekiq jobs not processing: Verify sidekiq service is running with 'docker compose ps' and check redis connection in Rails console
- Rails server binding to 127.0.0.1 instead of 0.0.0.0: Ensure server command includes '-b 0.0.0.0' flag for Docker container access
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download