【问题标题】:Setting up XDebug with Docker and VSCode使用 Docker 和 VSCode 设置 XDebug
【发布时间】:2021-08-30 13:40:37
【问题描述】:

我使用 Docker 搭建了一个 Laravel 开发环境 - nginx:stable-alpine、php:8.0-fpm-alpine 和 mysql:5.7.32。我从我的 php.dockerfile 安装 Xdebug:

RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS} \ 
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& apk del pcre-dev ${PHPIZE_DEPS}

并在 docker-compose 中包含两个卷以将 php 指向 xdebug.ini 和 error_reporting.ini:

    volumes:
  - .:/var/www/html
  - ../docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
  - ../docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini

我的 xdebug.ini 看起来像这样:

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug,trace,profile,coverage
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.remote_connect_back = 1 
xdebug.client_port = 9003
xdebug.remote_host='host.docker.internal'
xdebug.idekey=VSCODE

当我返回 phpinfo() 时,我可以看到一切设置正确,显示已安装 xdebug 版本 3.0.4,但是当我在 VSCode 中设置断点并运行调试器时,它没有命中它。

我的 launch.json 如下所示:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "XDebug Docker",
        "type": "php",
        "request": "launch",
        "port": 9003,
        "pathMappings": {
            "/var/www/html": "${workspaceFolder}/src",
        }
    },
]

}

我的文件夹结构如下:

- Project
-- /docker
--   nginx.dockerfile
--   php.dockerfile
--- /nginx
---  /certs
---  default.conf
--- /php
---- /conf.d
----   error_reporting.ini
----   xdebug.ini
-- /src (the laravel app)

【问题讨论】:

  • 您是在运行调试器还是脚本?您是否尝试过设置断点并运行脚本(通过 CLI 或浏览器)?
  • xdebug.remote_connect_back = 1 在 Xdebug 3 中不再存在。xdebug.remote_host='host.docker.internal' ← 删除单引号。 xdebug_info(); 在您尝试调试的脚本中显示什么?在 Linux 上,host.docker.internal 在没有 hack 的情况下无法工作 → youtube.com/watch?v=ZIGdBSD6zvU @ 1:10
  • 我赞同@Derick 他关于xdebug_info(); 的建议。 Host.docker.internal(不带引号)在 macOS 上对我来说是开箱即用的。
  • 啊,没错。因为 Xdebug 3 将其命名为 xdebug.client_host - 它仍然为您显示 localhost,因为这是默认值。:xdebug.org/docs/upgrade_guide#changed-xdebug.remote_host
  • 当时我也添加了这个作为正确答案,所以其他人可以找到它。

标签: docker visual-studio-code xdebug vscode-debugger xdebug-3


【解决方案1】:

Xdebug 3 更改了设置的名称。根据升级指南,您需要使用xdebug.client_host,而不是xdebug.remote_hosthttps://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_host

xdebug.remote_connect_back 也已重命名为 xdebug.discover_client_host,但在将 Xdebug 与 Docker 一起使用时,您应该将该设置保留为 0

【讨论】:

  • 当我运行 xdebug_info 我得到两个警告,我应该关心这些吗? ⚠️ [Step Debug] Creating socket for '172.31.0.1:9003', poll 成功,但报错:Operation in progress (29)。 ⚠️ [Step Debug] 无法连接到通过 HTTP 标头发现的客户端主机,连接到配置的地址/端口:host.docker.internal:9003。 :-|
  • 并非如此,但它暗示您将xdebug.discover_client_host 设置为1,这是您不应该这样做的。
  • xdebug.discover_client_host 设置为 0 - 参见上面的 xdebug.ini
  • 我不会担心它,除非它以某种方式引起问题。谢谢。
猜你喜欢
  • 2019-07-19
  • 2020-03-23
  • 2020-09-13
  • 1970-01-01
  • 2021-12-02
  • 2017-09-07
  • 2022-07-12
  • 2019-06-23
  • 2020-06-20
相关资源
最近更新 更多