概要
VSCodeでDocker Desktop(Windows)上のコンテナをデバッグする際、正しく設定しているつもりが以下のようなエラーが出てしまい正常に接続できない問題が起きていました。
この件について対処出来たためメモしておきます。
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(
TL; DR
VSCodeのlaunch.jsonに以下のhostname
の指定が必要でした。
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"hostname": "0.0.0.0", // この設定が必要
"port": 9003,
"pathMappings": {
"/app": "${workspaceRoot}"
},
"log": true
}
]
環境
今回は以下のような構成でRemote Containerは使用せず、WSL2上のVSCode ServerとDocker Desktop上のコンテナ間で
デバッグを行おうとする構成でした。
(あまり一般的ではない...?)
VSCode Serverから見るとhost.docker.internal(自分の環境では192.168.65.2)からの接続を許可する必要があるため、"0.0.0.0"の指定が必要だったのかと思っていますが、この部分はちょっと自信がありません。
xdebug.ini (XDebug3)
zend_extension=xdebug
[xdebug]
xdebug.mode=debug
xdebug.start_with_request = yes
xdebug.client_host=host.docker.internal
xdebug.log_level = 0 ;コネクション失敗のログを取りたい時は1、不要なら0などにする。https://xdebug.org/docs/all_settings#log_level