0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rails+Docker 構成を VSCode+rdbg でデバッグする

0
Last updated at Posted at 2026-04-30

なにこれ

  • 表題の要件に関して、生成 AI が一撃で答えを導けなかった(むしろミスリードされた)のでメモしておきます

やりたいこと

  • Docker 構成の Rails アプリをブラウザアクセス時と spec 実行時とでシームレスにデバッグしたい

解決した課題

  • 「spec 実行時、VSCode の "実行とデバッグ" で手動アタッチを毎回要求される問題」が最後まで残っていましたが、これを解決しました

前提

  • Ruby 4.0.1, Rails 8.1.2
  • VSCode の拡張機能 Ruby LSP, VSCode Ruby rdbg Debugger がインストール済み
  • compose.yml にて、コンテナ起動時に Rails Server を起動している

ソースコード

# Gemfile
group :development, :test do
  gem "debug", platforms: [ :mri ], require: "debug/prelude"
  # (...省略)
compose.yml
services:
  web:
    # (...省略)
    environment:
      # ここに RUBY_DEBUG_OPEN や RUBY_DEBUG_HOST や RUBY_DEBUG_PORT を書いてしまうとハマる
      # bin/rspec コマンドに環境変数の上書きを添えたが無駄だった
    stdin_open: true
    tty: true
    command: >
      bash -c "
        rm -f tmp/pids/server.pid &&
        bundle exec rdbg --open --host 0.0.0.0 --nonstop -n -- bin/rails s -p 3000 -b '0.0.0.0'
      "
.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "rdbg",
      "name": "Attach to Docker",
      "request": "attach",
      "localfs": true
    }
  ]
}

USAGE

  1. デバッグ対象に debugger の行を仕込む
  2. VSCode の「実行とデバッグ」で Attach to Docker を実行する(初回のみ)
  3. ブラウザアクセスすると VSCode 上でブレークする
  4. spec を実行すると、実行したターミナルでブレークする
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?