【问题标题】:VS Code PHP debugger does not work with DockerVS Code PHP 调试器不适用于 Docker
【发布时间】:2021-09-16 10:31:25
【问题描述】:

我正在尝试使用 Docker for Windows 和 VS Code 设置开发环境来调试 PHP 代码。

我已经从这个 Dockerfile 创建了一个 Docker 容器:

FROM php:8.0.10-apache

RUN pecl install xdebug-3.0.4 && \
    docker-php-ext-enable xdebug && \
    mkdir /var/log/php && \
    chown www-data:www-data /var/log/php && \
    chmod 755 /var/log/php && \
    { \
        echo ''; \
        echo '[xdebug]'; \
        echo 'xdebug.mode=develop,debug'; \
        echo 'xdebug.client_host=host.docker.internal'; \
        echo 'xdebug.start_with_request=yes'; \
        echo 'xdebug.log=/var/log/php/xdebug.log'; \
    } >> ${PHP_INI_DIR}/conf.d/docker-php-ext-xdebug.ini && \
    echo 'error_reporting=E_ALL' >> ${PHP_INI_DIR}/conf.d/error_reporting.ini

当我在 VS Code 上启动调试器并使用浏览器打开 PHP 文件时,即使我没有在 PHP 文件中放置任何断点,页面也会永远加载。

如果我在文件中放置断点,页面仍然会永远加载,并且调试器不会在断点处停止:跨步/进入/退出图标在 VS 代码上显示为灰色,变量表为空:

如果我在 VS Code 上停止调试器,则页面加载成功。

这些是 Xdebug 的日志:

[17] Log opened at 2021-09-16 10:39:03.887841
[17] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[17] [Step Debug] INFO: Connected to debugging client: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port). :-)
[17] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="8.0.10" protocol_version="1.0" appid="17"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[17] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

[17] Log closed at 2021-09-16 10:41:00.782334

[18] Log opened at 2021-09-16 10:41:10.195656
[18] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[18] [Step Debug] INFO: Connected to debugging client: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port). :-)
[18] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="8.0.10" protocol_version="1.0" appid="18"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[19] Log opened at 2021-09-16 10:42:26.969063
[19] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[19] [Step Debug] INFO: Connected to debugging client: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port). :-)
[19] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="8.0.10" protocol_version="1.0" appid="19"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[19] [Step Debug] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

[19] Log closed at 2021-09-16 10:42:31.116586

[20] Log opened at 2021-09-16 10:42:36.631851
[20] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[20] [Step Debug] ERR: Time-out connecting to debugging client, waited: 200 ms. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(
[20] Log closed at 2021-09-16 10:42:36.835780

[21] Log opened at 2021-09-16 10:42:38.915095
[21] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[21] [Step Debug] INFO: Connected to debugging client: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port). :-)
[21] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="8.0.10" protocol_version="1.0" appid="21"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

[1049] Log opened at 2021-09-16 10:42:40.727353
[1049] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[1049] [Step Debug] INFO: Connected to debugging client: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port). :-)
[1049] [Step Debug] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/html/test.php" language="PHP" xdebug:language_version="8.0.10" protocol_version="1.0" appid="1049"><engine version="3.0.4"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2021 by Derick Rethans]]></copyright></init>

这是 VS Code 的 launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/html/": "${workspaceFolder}"
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 0,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug,develop",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:0"
            ],
            "program": "",
            "cwd": "${workspaceRoot}",
            "port": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

【问题讨论】:

  • 这是完整的日志,还是只是其中的一部分?看起来 Xdebug 成功连接到 VS Code,但它可能决定不做任何事情?这可能暗示没有正确的路径映射。我在您的启动文件中没有看到任何配置。
  • 您是否尝试过添加路径映射?喜欢"pathMappings": {"/path/webroot/in/container":"/local/path/to/webroot"}
  • @Derick 这是完整的日志。我曾多次尝试连接到该页面并停止调试器(我已使用新日志更新了我的问题)。
  • @Sam020 是的,试过了,但没有结果。

标签: php visual-studio-code xdebug docker-for-windows


【解决方案1】:

我终于弄明白了为什么调试器不起作用:我在容器内启动了调试会话(我通过Remote - Containers VS Code extension 连接到它),而不是从 Docker 主机。所以,在这种情况下,xdebug.client_host 的正确值是localhost,因为 Xdebug 客户端和服务器都在同一个主机上。

为了使它与xdebug.client_host=host.docker.internal 一起工作,我设置了一个 Docker 卷,并直接在 Windows 上启动了调试器,而无需从 VS Code 内部连接到容器。在这种情况下,我还必须在 launch.json 文件中添加路径映射,它将 Docker 卷的路径映射到工作区文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2020-09-30
    • 2021-01-06
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多