如何使用pry在docker中调试rails应用程序?

时间:2016-02-04 21:07:25

标签: ruby-on-rails docker pry

我在开发环境中的docker容器中运行了一个rails应用程序。

当我尝试通过在代码中的某处放置binding.pry并附加到容器来调试它时,我可以看到输出中的pry提示符,但它并没有停留在它上面而我不能像没有docker容器一样与它进行交互。

那么我该如何调试容器化的应用程序?

6 个答案:

答案 0 :(得分:50)

如果您使用的是docker-compose,则可以将这些标记添加到docker-compose.yml

app:
  tty: true
  stdin_open: true

然后使用docker attach project_app_1附加到您的流程。 pry-rails现在在这里工作。确保您的容器上安装了less以获得最佳撬体验。

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

答案 1 :(得分:20)

使用pry你必须以不同的方式运行它:

docker-compose run --service-ports web

查看这篇文章了解更多信息:

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

答案 2 :(得分:5)

当我在Passenger中运行pry时,我遇到了同样的问题。尝试将Gemfile中的"pry-rails"更改为gem "pry-remote",这将启动dRuby或没有依赖关系的分布式协议。

您希望在执行调用"binding.remote_pry"中停止代码,而不是"binding.pry"

然后只需在控制台中调用remote-pry即可访问它。它应该工作相同。在您的测试环境中,通常的binding.pry工作正常。

答案 3 :(得分:4)

作为 Gabe Kopley 的答案,假设您的rails容器名为app,并将stdin_opentty设置为true

app:
  stdin_open: true
  tty: true

我写了一个bash脚本来简化生活,将其保存到bin/dev

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

不要忘记使dev可以执行chmod +x bin/dev

在您的终端中,键入bin/dev,它将自动运行容器并附加应用容器。调用binding.pry时,您可以直接输入终端。

答案 4 :(得分:1)

如果您不使用docker-compose,则只需使用-it选项运行容器即可。

例如:

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

答案 5 :(得分:0)

如果您是在ruby-*-alpine高山Linux下运行,则需要安装软件包ncurses,其中包括infocmp

如果这是原因,您将在错误日志中找到它:

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    

要解决此问题,请在您的Dockerfile上添加:

RUN apk add ncurses

尽管您可能已经有apk add行,所以只需在此处添加ncurses

那么你可以做