Running Multiple Rails Environments in Parallel
At Dexter, we use AI-powered coding assistants like Claude Code to handle well-defined coding tasks and quick prototypes. They give us a significant boost in productivity, but they’re slow, so it helps to run several of them in parallel.
When you do that, you’ll have to keep multiple versions of the codebase checked out at once in separate folders app/, app-2/, app-3/… This leads to two common problems:
- Database conflicts: all checkouts point to the same database.
- Port conflicts: all try to run on port 3000.
Here’s how to solve both in just a few lines of configuration.
1. Avoiding database conflicts
Even if you check out your app into multiple folders or worktrees, Rails will use the same development and test database names. If your agents run migrations in parallel, they’ll quickly step on each other.
The fix is to include the folder name in the database config. Update config/database.yml like this:
development:
<<: *default
database: <%= Rails.root.basename.to_s %>_development
test:
<<: *default
database: <%= Rails.root.basename.to_s %>_test
Now each checkout gets its own database:
app/→app_development,app_testapp-2/→app-2_development,app-2_testapp-3/→app-3_development,app-3_test
This change only affects development and test environments, not production.
2. Avoiding port conflicts
By default, each checkout will try to start the Rails server on port 3000. That doesn’t work if you’re running several instances at once.
To fix this, update your bin/dev to pick a port based on the folder name:
#!/usr/bin/env ruby
require "pathname"
if match = Pathname(__dir__).parent.basename.to_s.match(/(\d+)$/)
ENV['PORT'] = (3000 + match[1].to_i).to_s
end
exec "./bin/rails", "server", *ARGV
How it works:
- Running
bin/devinapp/starts the server on port 3000. - Running it in
app-2/starts on port 3002. - Running it in
app-3/starts on port 3003.
This way, you can run multiple servers simultaneously without conflicts.
Wrapping up
With these two tweaks:
- Each checkout or worktree has its own isolated database.
- Each checkout or worktree runs on its own port.
That means you can spin up multiple Rails environments in parallel, perfect for AI agents, and pretty handy for humans too.