ruby 使用puma和vscode在docker中调试rails应用程序

ia2d9nvy  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(139)

我正在尝试在rails应用程序上运行vscode调试器。rails应用程序在docker容器中运行,它使用puma config运行:
docker-compose命令:bundle exec puma -C config/puma.rb -b 'tcp://0.0.0.0:3000'
puma.rb配置

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
if ENV['RACK_ENV'] == 'development'
  worker_timeout 3600 # 1 hour to prevent debugger timeouts
end

before_fork do
  Barnes.start # Initializes runtime metrics
end

vscode配置

{
      "type": "rdbg",
      "name": "Attach with rdbg",
      "request": "attach",
      "debugPort": "localhost:12345",
    }

我尝试了许多变化rdbg,调试等。
但还不能让它工作

cedebl8k

cedebl8k1#

你需要确保这些事情:
1.配置Docker以使用调试器启动服务。在应用根目录下创建scripts/entrypoint.sh脚本:

#! /bin/bash
rdebug-ide --host 0.0.0.0 \
           --port 1234 \
           --skip_wait_for_start  -- \
           bundle exec puma -C config/puma.rb -b 'tcp://0.0.0.0:3000'

i.如果bundle不工作,请使用二进制的完整路径,例如/usr/local/bundle/bin/bundle阅读此处以了解rdebug-ide中的所有可用选项:rdebug-ide github
ii.确保ruby-debug-idedebase gems安装在容器内和机器上。
1.在你的docker compose中:

services:
  app:
    image: demo:latest
    working_dir: "/opt/app/demo"
    command: ["scripts/entrypoint.sh"]
    depends_on:     
      - redis
    ports: 
      - "3000:3000"
      - "1234:1234" # <-------- This is important, make sure to expose the debugger port

1.在项目根目录中创建一个.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "Ruby",
            "request": "attach",
            "name": "Attach to container",
            "remoteWorkspaceRoot": "/opt/app/demo", // <------ path inside container
            "showDebuggerOutput": true,
            "remotePort": "1234",  // <---------- port exposed in compose.yaml file
            "cwd": "${workspaceRoot}"
        }
    ]
}

i.如果你使用的是Intellij,那么在dispatcherPort param中放入任意随机整数值。
ii.确保在VScode中安装了RubyVS Code ruby debugger扩展
iii.如果您使用rbenv,请相应修改上述扩展选项。(Ctrl+Shift+P > Open user settings json)

"rdbg.rubyVersionManager": "rbenv",
    "ruby.interpreter.commandPath": "/usr/local/home/atulvaibhav/.rbenv/shims/ruby",

这应该够了我在集群模式下使用puma时遇到了一些问题,所以我修改了puma.rb,使其只在单一模式下运行,方法是注解掉该文件中与workers相关的设置。
现在你应该都准备好了。把你的容器拿上来:compose up -d>然后转到VS-code并启动名为Attach to container的调试器

相关问题