Xdebug连接VSC与WSL2和Docker

qhhrdooz  于 2023-03-29  发布在  Docker
关注(0)|答案(2)|浏览(143)

我试图让Xdebug在我的本地机器上工作,但遇到了以下问题:Xdebug没有连接到VSCode。
我的安装程序是在Windows 10上的WSL和Visual Studio代码上使用Docker容器。
当我尝试Xdebug时,我得到以下错误:

[Step Debug] Creating socket for '192.168.16.1:9004', poll success, but error: Operation now in progress (29).  🖹
⚠️  [Step Debug] Could not connect to client host discovered through HTTP headers, connecting to configured address/port: localhost:9004. :-|   🖹
⚠️  [Step Debug] Creating socket for 'localhost:9004', poll success, but error: Operation now in progress (29). 🖹
⚠️  [Step Debug] Creating socket for 'localhost:9004', connect: Cannot assign requested address.    🖹
🛑  [Step Debug] Could not connect to debugging client. Tried: 192.168.16.1:9004 (from REMOTE_ADDR HTTP header), localhost:9004 (fallback through xdebug.client_host/xdebug.client_port) :-(`

这些是我的设置:
launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9004,
            "stopOnEntry": true,
            "log": true,
            "hostname" : "localhost",
            "pathMappings": 
            {
                "/var/www/html/": "${workspaceRoot}/docker/wordpress/public/"             
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9004
        }
    ]  
}

php.ini

[xdebug]
#zend_extension = ./lib/php/20180731/xdebug.so
xdebug.start_with_request = trigger 
xdebug.mode = debug
xdebug.discover_client_host = 1
xdebug.log = /tmp/xdebug_remote.log
xdebug.client_port = 9004

Dockerfile

RUN apt-get update \
    && apt-get install -y \
    zlib1g-dev \
    libicu-dev \
    g++ \
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl \
    && touch /usr/local/etc/php/conf.d/uploads.ini \
    && echo "memory_limit = 128M;\nupload_max_filesize = 64M\npost_max_size = 128M" >> /usr/local/etc/php/conf.d/uploads.ini

RUN docker-php-ext-install pdo pdo_mysql \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug

我已经测试了端口9003,它不工作。当我检查是否有可用的telnet连接时,它会连接。我还在防火墙上启用了端口。
编辑:我已经找到了一个配置,为我工作的基础上提供的链接从@LazyOne。
我更新了以下配置并使用了浏览器扩展。
launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "stopOnEntry": true,
            "log": true,
            "host" : //wsl ip
            "pathMappings": 
            {
                "/var/www/html/": "${workspaceRoot}/docker/wordpress/public/"             
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9003
        }
    ]  
}

php.ini

[xdebug]
zend_extension = ./lib/php/20180731/xdebug.so
xdebug.start_with_request = trigger 
xdebug.mode = debug
xdebug.discover_client_host = 0
xdebug.log = /tmp/xdebug_remote.log
xdebug.client_port = 9003
xdebug.client_host = //wsl ip
tjrkku2a

tjrkku2a1#

这些消息中的每一个都有一个到文档的链接(右边的图标),你读过所有的吗?
现在,Xdebug告诉您它无法连接。这可能是因为IDE没有监听,或者是因为Xdebug试图连接到错误的IP地址。
xdebug.discover_client_host几乎不适用于WSL2(和Docker),因此您需要使用xdebug.client_host来设置正确的IP地址,并设置xdebug.discover_client_host=no
“正确”的IP地址是什么,将取决于您的设置,但IIRC,您可以在Windows命令行(而不是WSL2)上使用ipconfig /all来找出答案。

6psbrbz9

6psbrbz92#

我一直在为这个设置而挣扎。我遇到了一个类似的问题/错误,我将在这里发布它,因为这个问题在谷歌上很好。我最近发现的是,当我在vscode中运行docker compose up时,当我没有运行Listen for XDebug时,我在Xdebug中得到了这个错误:

[Step Debug] Creating socket for 'host.docker.internal:9003', poll success, but error: Operation now in progress (29).
[Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port).

但是如果我运行“Listen for Xdebug”,当我启动容器时,它会连接,没有问题。然后我可以开始和停止在vscode中监听Xdebug。
这种行为可能只是由于我的设置中的错误配置,但它可能会帮助别人,我只是如此兴奋,能够在我的设置上持续运行Xdebug。

相关问题