Debugging Rails with pry within a Docker container

If you are running your rails apps in production using docker, you may be tempted to still run locally outside of docker. One of the big reasons to potentially hold back is the use of pry.

However, its actually very simple to attach to a running docker process which makes using pry a breeze.

 

First, ensure that you have the following lines in your docker-compose file for the service you want to attach to:

Now, rebuilding your container by using docker-compose up -d  which should recreate the container if needed.

Now, use docker ps  to find the container id and then docker attach  in order to attach to the running process.

Now, you can insert a binding.pry in your code somewhere…

 

You can now perform your usual debug commands. When done debugging type exit to leave the pry debug session.

To detach from the container without exiting press control + p  and the control + q . Note that if you hit control + c instead of the escape sequence the container process will exit.

Leave a Reply

Your email address will not be published. Required fields are marked *