0
0
RailsHow-ToBeginner · 4 min read

How to Deploy Ruby on Rails Using Docker: Step-by-Step Guide

To deploy a Ruby on Rails app using Docker, create a Dockerfile to define the app environment and a docker-compose.yml to manage services like the database. Build the Docker image and run containers to launch your Rails app in isolated, consistent environments.
📐

Syntax

The deployment setup involves two main files: Dockerfile and docker-compose.yml.

  • Dockerfile: Defines the Ruby and Rails environment, installs dependencies, copies app code, and sets the command to start the server.
  • docker-compose.yml: Defines services like web (Rails app) and db (PostgreSQL), sets ports, volumes, and environment variables.
Dockerfile
FROM ruby:3.1

# Install dependencies
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

# Set working directory
WORKDIR /app

# Copy Gemfiles and install gems
COPY Gemfile Gemfile.lock ./
RUN bundle install

# Copy the rest of the app
COPY . ./

# Precompile assets (optional for production)
# RUN bundle exec rake assets:precompile

# Expose port 3000
EXPOSE 3000

# Start the Rails server
CMD ["rails", "server", "-b", "0.0.0.0"]
💻

Example

This example shows a simple Dockerfile and docker-compose.yml to deploy a Rails app with PostgreSQL. It demonstrates building the image, running the database, and starting the Rails server accessible on port 3000.

Dockerfile and YAML
### Dockerfile
FROM ruby:3.1

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . ./
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

---

### docker-compose.yml
version: '3.8'
services:
  db:
    image: postgres:14
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    volumes:
      - pgdata:/var/lib/postgresql/data
  web:
    build: .
    command: rails server -b 0.0.0.0
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      DATABASE_URL: postgres://postgres:password@db:5432/postgres
volumes:
  pgdata:
Output
Creating network "app_default" with the default driver Creating volume "app_pgdata" with default driver Creating app_db_1 ... done Creating app_web_1 ... done => Booting Puma => Rails 7.0.4 application starting in development => Run `bin/rails server --help` for more startup options Puma starting in single mode... * Version 5.6.4 (ruby 3.1.2-p20) * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://0.0.0.0:3000 Use Ctrl-C to stop
⚠️

Common Pitfalls

  • Missing dependencies: Not installing Node.js or PostgreSQL client in the Dockerfile causes asset compilation or DB connection failures.
  • Database connection errors: Incorrect DATABASE_URL or missing depends_on in docker-compose.yml can prevent Rails from connecting to the database.
  • Port conflicts: Ensure port 3000 is free or mapped correctly to avoid server startup errors.
  • Volume mounting issues: Mounting the app directory incorrectly can cause stale code or permission problems.
Dockerfile
### Wrong Dockerfile snippet (missing nodejs)
RUN apt-get update -qq && apt-get install -y postgresql-client

### Correct Dockerfile snippet
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
📊

Quick Reference

Remember these key points when deploying Rails with Docker:

  • Use Ruby official images matching your Rails version.
  • Install Node.js for JavaScript and asset compilation.
  • Use docker-compose to manage multi-service apps (web + db).
  • Set environment variables for database connection.
  • Expose and map ports to access the app from your browser.

Key Takeaways

Create a Dockerfile to define your Rails app environment including Ruby, Node.js, and dependencies.
Use docker-compose.yml to run Rails and PostgreSQL services together with proper environment variables.
Always install required system packages like Node.js and PostgreSQL client inside the Docker image.
Map ports and volumes correctly to access the app and enable live code updates during development.
Check database connection settings and service dependencies to avoid startup errors.