Using Pry in Docker or Foreman with Rails

Moving to Docker or Foreman for rails development requires learning some new debugging and testing strategies. If you're like me, you drop binding.pry in every feature you build at one point or another.

If you are using Docker or Foreman for local development in Rails, then you've probably noticed that pry doesn't work very well in these environments. I haven't done a lot of research into why this is the case, (maybe some one else can chime in), but to get pry running follow these steps:

1. Install gems

Add the following gems and fire up your server:

gem 'pry-rails'
gem 'pry-remote'
gem 'pry-nav'

You can read more about them in their respective docs.

2. Using pry-remote

Instead of using binding.pry drop binding.pry_remote whereever you need it. The app will halt, but won't open an interactive session automatically.

3. Open a new Terminal

In a new terminal session, under the root of your app enter the pry remote command:

$ pry-remote

And you're new terminal will tap into the breakpoint you set and you can pry away.

This takes a bit of an adjustment, but the inconvenience pales in comparison to the benefits of using Docker or Foreman in development.