How to debug a rails app in docker with pry?

Ruby on-RailsDockerPry

Ruby on-Rails Problem Overview


I have a rails app running in a docker container in development environment.

When I try to debug it with placing binding.pry somewhere in the code and attaching to the container I can see the pry prompt in the output but it doesn't pause on it and I can't interact with it like it was without docker container.

So how do I debug a containerized app?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

If you're using docker-compose, you can add these flags to docker-compose.yml:

app:
  tty: true
  stdin_open: true

And then attach to your process with docker attach project_app_1. pry-rails works here now. Ensure less is installed on your container for the optimal pry experience.

cf. https://github.com/docker/compose/issues/423#issuecomment-141995398

Solution 2 - Ruby on-Rails

to use pry you have to run it differently:

docker-compose run --service-ports web

check out this article for more info:

http://blog.carbonfive.com/2015/03/17/docker-rails-docker-compose-together-in-your-development-workflow/

Solution 3 - Ruby on-Rails

as Gabe Kopley answer, assume your rails container is called app, set stdin_open and tty to true :

app:
  stdin_open: true
  tty: true

and I wrote a bash script to make life easier, save it to bin/dev:

#!/bin/bash
docker-compose up -d && docker attach $(docker-compose ps -q app)

don't forget to make dev be executable by chmod +x bin/dev

In your terminal, type bin/dev, it will automatically run up containers and attach the app container. when binding.pry called, you can type to the terminal directly.

Solution 4 - Ruby on-Rails

I had this same issue when I was running pry in Passenger. Try changing "pry-rails" in the Gemfile to gem "pry-remote", which will initiate a dRuby, or distributed protocol with no dependencies.

Where you want to stop the code in execution call "binding.remote_pry" as opposed to "binding.pry"

Then just call remote-pry in the console to access it. It should work the same. In your test environment just the usual binding.pry works fine.

Solution 5 - Ruby on-Rails

If you don't use docker-compose though, you can simply run the container with -it option.

For example:

docker run -v /Users/adam/Documents/Rails/Blog/:/usr/src/app -p 3000:3000 -it blog

Solution 6 - Ruby on-Rails

If you are running in under alpine linux aka ruby-*-alpine you need to install package ncurses wich includes infocmp.

If that's the reason you will have this in your error log:

bin/rails: No such file or directory - infocmp                                                              
Error: undefined method `split' for nil:NilClass 
...
bin/rails:4:in `<main>'                                                                                                                 
FATAL: Pry failed to get user input using `Readline`.                                                       
To fix this you may be able to pass input and output file descriptors to pry directly. e.g.                                    
  Pry.config.input = STDIN                                                                                   
  Pry.config.output = STDOUT                                                                           
  binding.pry    

To fix it, on your Dockerfile add:

RUN apk add ncurses

Although You probably already had an apk add line, so just add ncurses there.

then you can do

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfeyView Question on Stackoverflow
Solution 1 - Ruby on-RailsGabe KopleyView Answer on Stackoverflow
Solution 2 - Ruby on-RailsTerri ChuView Answer on Stackoverflow
Solution 3 - Ruby on-RailsYi Feng XieView Answer on Stackoverflow
Solution 4 - Ruby on-RailskalycoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAdam SibikView Answer on Stackoverflow
Solution 6 - Ruby on-RailsArnold RoaView Answer on Stackoverflow