docker.recipes

Ruby on Rails + PostgreSQL

intermediate

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: rails
5 command: bundle exec rails server -b 0.0.0.0
6 volumes:
7 - .:/app
8 environment:
9 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
10 REDIS_URL: redis://redis:6379/0
11 ports:
12 - "3000:3000"
13 depends_on:
14 - postgres
15 - redis
16 networks:
17 - rails
18
19 postgres:
20 image: postgres:16-alpine
21 container_name: postgres
22 environment:
23 POSTGRES_DB: ${DB_NAME}
24 POSTGRES_USER: ${DB_USER}
25 POSTGRES_PASSWORD: ${DB_PASSWORD}
26 volumes:
27 - postgres_data:/var/lib/postgresql/data
28 networks:
29 - rails
30
31 redis:
32 image: redis:alpine
33 container_name: redis
34 networks:
35 - rails
36
37 sidekiq:
38 build: .
39 command: bundle exec sidekiq
40 environment:
41 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
42 REDIS_URL: redis://redis:6379/0
43 depends_on:
44 - postgres
45 - redis
46 networks:
47 - rails
48
49volumes:
50 postgres_data:
51
52networks:
53 rails:
54 driver: bridge

.env Template

.env
1DB_NAME=rails
2DB_USER=rails
3DB_PASSWORD=changeme

Usage Notes

  1. 1Docs: https://guides.rubyonrails.org/
  2. 2Create Dockerfile with Ruby + bundler
  3. 3Run migrations: docker compose exec web rails db:migrate
  4. 4Access at http://localhost:3000
  5. 5Rails console: docker compose exec web rails c
  6. 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 file
2cat > docker-compose.yml << 'EOF'
3services:
4 web:
5 build: .
6 container_name: rails
7 command: bundle exec rails server -b 0.0.0.0
8 volumes:
9 - .:/app
10 environment:
11 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
12 REDIS_URL: redis://redis:6379/0
13 ports:
14 - "3000:3000"
15 depends_on:
16 - postgres
17 - redis
18 networks:
19 - rails
20
21 postgres:
22 image: postgres:16-alpine
23 container_name: postgres
24 environment:
25 POSTGRES_DB: ${DB_NAME}
26 POSTGRES_USER: ${DB_USER}
27 POSTGRES_PASSWORD: ${DB_PASSWORD}
28 volumes:
29 - postgres_data:/var/lib/postgresql/data
30 networks:
31 - rails
32
33 redis:
34 image: redis:alpine
35 container_name: redis
36 networks:
37 - rails
38
39 sidekiq:
40 build: .
41 command: bundle exec sidekiq
42 environment:
43 DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
44 REDIS_URL: redis://redis:6379/0
45 depends_on:
46 - postgres
47 - redis
48 networks:
49 - rails
50
51volumes:
52 postgres_data:
53
54networks:
55 rails:
56 driver: bridge
57EOF
58
59# 2. Create the .env file
60cat > .env << 'EOF'
61DB_NAME=rails
62DB_USER=rails
63DB_PASSWORD=changeme
64EOF
65
66# 3. Start the services
67docker compose up -d
68
69# 4. View logs
70docker compose logs -f

One-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 | bash

Troubleshooting

  • 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