composebuilder

Docker Compose: Rails + PostgreSQL + Redis

Run a Ruby on Rails app with PostgreSQL 16 and Redis 7 in Docker Compose, with a slim Ruby 3.3 Dockerfile, database setup, persistent data and a checklist.

When to use this stack

This stack runs a Ruby on Rails application on Ruby 3.3 with PostgreSQL 16 as the main database and Redis 7 for the cache and real-time features such as Action Cable. Rails already expects a database, a cache and a secret key, and Compose makes those three pieces start together with the same settings on every machine. The Dockerfile keeps the image slim and installs only the system libraries needed to compile the PostgreSQL gem, which avoids the common problem of a Ruby installation that works on one laptop and fails on another.

Choose it when you are building a web product with Rails conventions, want a full-stack framework with migrations and a strong ecosystem, and plan to host on one VPS or a small cluster. It is a good fit for SaaS prototypes, internal business tools, marketplaces and content platforms. Do not choose this layout if you need to run dozens of background worker processes across many machines, or if your team needs a managed platform with automatic scaling. In that case, use the same image with a platform that builds Dockerfiles directly.

The Dockerfile starts from the official ruby:3.3-slim image, installs bundle dependencies without development and test gems, and runs Puma through bin/rails server. If your project has no asset pipeline, you can remove the assets:precompile line. The compose file sets RAILS_ENV to production, so Rails reads its database settings from DATABASE_URL and its cache and Action Cable settings from REDIS_URL.

docker-compose.yml

services:
  web:
    build: ./app
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: production
      RAILS_LOG_TO_STDOUT: "1"
      RAILS_SERVE_STATIC_FILES: "1"
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
      REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379/0

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
  redis_data:

Dockerfile (app/Dockerfile)

Place this file in ./app, next to the Gemfile. Add a .dockerignore with .git, log, tmp, node_modules and .env.

Dockerfile
FROM ruby:3.3-slim

ENV RAILS_ENV=production \
    BUNDLE_WITHOUT="development test"

WORKDIR /rails

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

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY . .

# Remove this line if the app has no asset pipeline
RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile

EXPOSE 3000
CMD ["bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]

.env

.env
# PostgreSQL database, user and password
POSTGRES_DB=app_production
POSTGRES_USER=rails
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Redis password
REDIS_PASSWORD=CHANGE_ME_redis_password
# Generate with: bin/rails secret (run in a local copy of the project)
SECRET_KEY_BASE=CHANGE_ME_run_bin_rails_secret

Start and verify

Shell
docker compose config --quiet

# Build the image and start the stack
docker compose up -d --build

# db and redis must be healthy before web starts
docker compose ps

# Create the database and run pending migrations (safe to repeat)
docker compose exec web bin/rails db:prepare

# Read the Rails log for "Listening on" and any errors
docker compose logs --tail=50 web

# The app should answer; a 200 or a 302 redirect are both normal
curl -I http://localhost:3000

Production checklist

  • Serve Rails behind HTTPS with a reverse proxy, and keep config.force_ssl enabled in production.
  • Keep SECRET_KEY_BASE in .env or your secret manager, never in the repository, and use a different value for each environment.
  • Use Rails credentials or environment variables for every API key. Do not hard-code them in initializers.
  • Keep RAILS_LOG_TO_STDOUT on, so logs are available with docker compose logs and to your log shipper.
  • Run db:prepare or db:migrate as a deliberate deploy step, and back up postgres_data before each migration.
  • Use URL-safe passwords for POSTGRES_PASSWORD and REDIS_PASSWORD, because they are embedded in DATABASE_URL and REDIS_URL.
  • Keep Redis internal and persistent with –appendonly, and do not publish port 6379.
  • Set a memory limit for the web container and tune Puma threads with RAILS_MAX_THREADS if the host is small.

Common problems

  • Symptom: The build fails while compiling the pg gem. Cause: libpq-dev or a compiler is missing from the image. Fix: Keep build-essential and libpq-dev in the Dockerfile, and rebuild with docker compose build –no-cache web.
  • Symptom: “ActiveRecord::PendingMigrationError” when you open the site. Cause: Migrations were not run after the new release. Fix: Run docker compose exec web bin/rails db:migrate, then reload the page.
  • Symptom: “key must be 64 bytes” or sessions break. Cause: SECRET_KEY_BASE is empty or too short. Fix: Generate a new value with bin/rails secret and recreate the web container.
  • Symptom: Assets return 404 in production. Cause: The assets were not precompiled, or RAILS_SERVE_STATIC_FILES is not set. Fix: Keep the precompile line in the Dockerfile and keep RAILS_SERVE_STATIC_FILES set to 1, or serve public/ from the proxy.
  • Symptom: Action Cable connections fail with a Redis error. Cause: REDIS_URL has the wrong password or host name. Fix: Use redis://:password@redis:6379/0 with the same value as REDIS_PASSWORD.

Variations

  • Background jobs with Sidekiq: Add a worker service that uses the same image and runs bundle exec sidekiq. It needs REDIS_URL, and it should run with the same SECRET_KEY_BASE.
  • Solid Queue in the database: Rails 8 can run jobs in PostgreSQL with Solid Queue. In that case you do not need Redis for jobs, and the worker runs bin/jobs.
  • Thruster or Nginx in front: Put a proxy service in front of web to handle HTTPS and static files, and remove the public 3000 mapping so only the proxy listens on 80 and 443.

Questions

Which folder must contain my Rails project?

The web service builds from ./app, so the Gemfile, Gemfile.lock, bin folder and config folder must be in that folder. The code is copied into the image at build time.

Where does SECRET_KEY_BASE come from?

Run bin/rails secret in a local copy of the project and paste the output into .env. Rails uses it to sign cookies and encrypt data, so do not reuse it across environments.

Do I need Sidekiq for background jobs?

No. Rails has Active Job with several adapters. This stack uses Redis for the cache and Action Cable. Add a Sidekiq or Solid Queue worker service if your app runs background jobs.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Django + PostgreSQL + Celery

    Run a Django app with Gunicorn, PostgreSQL 16, Redis 7 as the Celery broker and a Celery worker in Docker Compose, with healthchecks and a production checklist.

    • web
    • worker
    • db
    • redis
    Difficulty: Advanced
  • Laravel + PostgreSQL + Redis

    Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Next.js + PostgreSQL + Redis

    Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Directus + PostgreSQL

    Run Directus 11 headless CMS on PostgreSQL 16 in Docker Compose, with required keys, admin bootstrap, persistent uploads, a health check and a checklist.

    • directus
    • db
    Difficulty: Intermediate

Browse more Web & Frameworks stacks or the full library.