LoginSignup
15
9

More than 3 years have passed since last update.

VSCode+Dockerコンテナ内のPHPをデバッグする際、host.docker.internalに接続できない問題の対処

Posted at

概要

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"の指定が必要だったのかと思っていますが、この部分はちょっと自信がありません。

image.png

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
15
9
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
15
9