Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using pry-rails with Docker

First, add pry-rails to your Gemfile:
https://github.com/rweng/pry-rails

gem 'pry-rails', group: :development

Then you'll want to rebuild your Docker container to install the gems

docker-compose build

You should now be able to add a break point anywhere in your code

binding.pry

The problem is that Docker just kind of ignores it and moves on. In order to actually halt execution and use pry as normal, you have to these options to docker-compose.yml:

app:
  tty: true
  stdin_open: true

Source: http://stackoverflow.com/a/37264588/1042144

The next time binding.pry is executed, the process should halt and display an IRB-like console on the rails server screen. But you still can't use it directly. You have to attach a terminal to the docker container.

In order to attach to a docker container, you need to know what its ID is. Use docker ps to get a list of the running containers and their ids.

docker ps

Source: https://www.liquidweb.com/kb/how-to-list-and-attach-to-docker-containers/

Then you can use the numeric ID to attach to the docker instance:

docker attach 75cde1ab8133

It may not immediately show a rails console, but start typing and it should appear. If you keep this attached, it should show the rails console prompt the next time you hit the pry breakpoint.

Neat things you should try with pry:

show-routes
show-models
show-source edit  # If you're in a controller, for instance

cd                # Navigate up or down class hierarchies. try `cd BasicObject`
ls                # Check out what's inside of a class - variables, etc.

And of course you can access local variables just by typing their names.

params
#=> <ActionController::Parameters {"controller"=>"lists", "action"=>"index"} permitted: false>

To end your pry-rails session, you can type exit as you would exit any irb instance.

exit

However, this does not detach the terminal from the docker container. This can be useful to continue debugging.

Additionally, I had two terminals open, one to review the server log and one to attach to my container to use pry-rails, but docker will timeout the server log while the attached terminal continues to run the rails server process. I'm okay with that - I can just leave the terminal attached. However, if you want to detach from the container without ending the process, you need to press ctrl-p + ctrl-q.

Source: http://stackoverflow.com/a/19689048/1042144

@NicoSa

This comment has been minimized.

Copy link

NicoSa commented Mar 7, 2018

Thanks for making this. Really cool, for some reason it just won't work for me though. No idea why. I am able to do all the steps but the pry dialog just never pops up anywhere even though it does definitely stop the app.

@supremebeing7

This comment has been minimized.

Copy link

supremebeing7 commented Mar 26, 2018

Definitely helpful. I'm seeing the same thing as @NicoSa where the pry dialog doesn't show up, but weirdly I can type something, hit enter, and it will then display the prompt, my command, and the result.

Maybe I'm missing some critical package?

@omaressameldin

This comment has been minimized.

Copy link

omaressameldin commented Apr 20, 2018

I am having a problem with this. The binding works and everything but when i press the arrow up key for example or any arrow key i dont get the command but ^[[A is there a way to fix that?

@bluciam

This comment has been minimized.

Copy link

bluciam commented Jun 21, 2018

I think your problem @supremebeing7 is that you seem to have an ActionCable of something like that which will continue to output to the stdout regardless of the breakpoint.

@tettttsuo

This comment has been minimized.

Copy link

tettttsuo commented Aug 10, 2018

I have the same issue with @supremebeing7. Have you or anyone solved the problem?
On the other hand, I don't have the issue mentioned by @omaressameldin.

@tenebrousedge

This comment has been minimized.

Copy link

tenebrousedge commented Aug 28, 2018

@tettttsuo You could disable ActionCable logging in development.rb:

ActionCable.server.config.logger = Logger.new(nil)
@dmtroyer

This comment has been minimized.

Copy link

dmtroyer commented Oct 31, 2018

I tried this and docker still just blows past the breakpoint.

@andrewpatterson3001

This comment has been minimized.

Copy link

andrewpatterson3001 commented Jul 19, 2019

While this worked for me previously, it has recently stopped responding to commands like next once it stops at the binding.pry line.

@litewhat

This comment has been minimized.

Copy link

litewhat commented Dec 23, 2019

Works for me! Thanks for it!

@ryanzidago

This comment has been minimized.

Copy link

ryanzidago commented Feb 23, 2020

Alternatively, granted that you have several docker services, let's say one named db and one named web, you could do this :

  • docker-compose up db (or add any subsequent services where you won't need an interactive shell)
  • docker-compose run --service-ports web.

The command docker-compose run --service-ports web will allow the interactive shell with pry/byebug and you won't need to create a new terminal tab, list all of the running containers and attach to the corresponding.

Thanks to @denispasin for the tip!

@briankung

This comment has been minimized.

Copy link
Owner Author

briankung commented Feb 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.