How to set up a Rails development environment with Docker
Getting a Rails application’s codebase running shouldn’t be difficult. Yet developers spend hours troubleshooting environment issues. Docker solves this.
Why Docker for Development?
- Consistent environments - Works the same on every machine
- Easy onboarding - New devs run one command
- Isolated dependencies - No conflicts with other projects
- Production parity - Develop in an environment similar to production (pair this with deployment tracking via
Rails.app.revisionto know exactly which code is running)
Basic Setup
Create a Dockerfile:
FROM ruby:3.2
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
Create a docker-compose.yml:
version: '3.8'
services:
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails server -b 0.0.0.0"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_URL: postgres://postgres:password@db/app_development
volumes:
postgres_data:
Running Your App
docker compose build
docker compose up
Visit http://localhost:3000 and you’re ready to develop.
Tips
- Use volume mounts for live code reloading
- Add Redis, Sidekiq as needed
- Create a
bin/docker-devscript for common commands
Docker removes the “it works on my machine” problem and lets your team focus on building features. Once your containerized environment is running, keep an eye on version-specific behavior like the connection pool changes introduced in Rails 7.2 that can affect performance.