Techioz Blog

puma と vscode を使用して Docker 内で Rails アプリをデバッグします

概要

Rails アプリで vscode デバッガーを実行しようとしています。 Rails アプリは Docker コンテナ内で実行され、puma config で実行されます。

docker-compose コマンド: バンドル実行 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、debugなど、さまざまなバリエーションを試しました。

しかし、まだ機能させることができません

解決策

次のことを確認する必要があります。

#! /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'

私。バンドルが機能しない場合は、バイナリのフルパス (例: /usr/local/bundle/bin/bundle) を使用してください。 rdebug-ide で利用可能なすべてのオプションについては、ここをお読みください:rdebug-ide github

  1. Ruby-debug-ide と debase gem がコンテナ内とマシンにインストールされていることを確認してください。
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
{
    "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}"
        }
    ]
}

私。 Intellij を使用している場合は、dispatcherPort パラメータにランダムな整数値を入力します。

  1. Ruby および VS Code の Ruby デバッガー拡張機能が VScode にインストールされていることを確認します。

  2. rbenv を使用する場合は、上記の拡張オプションを適宜変更してください。 (Ctrl+Shift+P > ユーザー設定 JSON を開く)

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

これで十分です。 puma をクラスター モードで使用するときにいくつかの問題に直面したため、ファイル内のワーカー関連の設定をコメント アウトすることにより、puma.rb をシングル モードでのみ実行するように修正しました。

これで準備は完了です。コンテナーを起動します: compose up -d > 次に、VS-code に移動し、Attach to Container という名前のデバッガーを起動します。