LoginSignup
2
0

More than 1 year has passed since last update.

debug.gemでDocker上のRubyをデバッグしたい

Posted at

やりたいこと

ruby/debug: Debugging functionality for Ruby

を使って、Dockerコンテナ上で起動したプログラムをデバッグする

参考

方法

各環境で下のようなコマンドを実行する。
このとき、利用したいコンテナ側のポートを公開しておく。

# コンテナ側
$ rdbg -O --host=0.0.0.0 --port={任意のポート} main.rb
# ホスト側
$ rdbg -A localhost {任意のポート}

サンプル

準備

# 構成
$ tree app
app
└── main.rb
main.rb
a = 1
b = 2
puts a
puts b
c = a + b
puts c
c += 3
puts c

検証

コンテナ側

# コンテナを起動
$ docker run -it -p 1234:1234 -v $PWD/app:/app --rm ruby:3.0.3-bullseye /bin/bash
# コンテナ内
## debugをインストール
$ gem install debug
## debugの起動
$ cd /app
$ rdbg -O --host=0.0.0.0 --port=1234 main.rb
DEBUGGER: Debugger can attach via TCP/IP (0.0.0.0:1234)
DEBUGGER: wait for debugger connection...
DEBUGGER: Connected.
1
2
3
6
DEBUGGER: Disconnected.

ホスト側

$ rdbg -A localhost 1234
[1, 8] in main.rb
=>   1| a = 1
     2| b = 2
     3| puts a
     4| puts b
     5| c = a + b
     6| puts c
     7| c += 3
     8| puts c
=>#0    <main> at main.rb:1
2
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
2
0