我在开发环境中的docker容器中运行了一个rails应用程序。
当我尝试通过在代码中的某处放置binding.pry
并附加到容器来调试它时,我可以看到输出中的pry
提示符,但它并没有停留在它上面而我不能像没有docker容器一样与它进行交互。
那么我该如何调试容器化的应用程序?
答案 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
查看这篇文章了解更多信息:
答案 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_open
和tty
设置为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
。
那么你可以做