【问题标题】:PHP XDebug on Docker Containers do not works in VS Code (Laravel and docker-compose)Docker 容器上的 PHP XDebug 在 VS Code 中不起作用(Laravel 和 docker-compose)
【发布时间】:2019-06-07 17:46:22
【问题描述】:

我无法使用 Visual Studio 代码和 Xdebug 在我的 docker-compose 容器环境中运行 php 调试器。

我尝试了article 中的所有步骤以及this 或this 和this 等各种问题

我有这个docker-compose.yml 文件(php 容器部分):

version: '2'
services:
    php:
        build: 
            context: images/php
        ports: 
            - "9000:9000"
        volumes:
            - ./www:/var/www
        links:
            - mysql
        depends_on: 
            - mysql
        network_mode: "bridge"

./www 卷是我的 Laravel 根应用程序路径,挂载到 php 容器:

这是我安装 Xdebug 的php Dockerfile 部分:

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.default_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_enable=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_connect_back=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_autostart=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_handler="dbgp"' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_host=0.0.0.0' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.max_nesting_level=250' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_log=/var/www/xdebug.log' >> /usr/local/etc/php/conf.d/xdebug.ini \

当我构建我的容器时,我没有错误,并将一个 shell 附加到 php 容器并运行 php -v 我有这个输出:

PHP 7.2.3 (cli) (built: Mar 22 2018 22:03:09) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans

因此,Xdebug 已安装到容器中。我安装了 PHP-debug 扩展并设置了这个 VSCode 调试配置:

{
    "version": "0.2.0",
    "configurations": [{
        "name": "Listen for XDebug on Docker",
        "type": "php",
        "request": "launch",
        "port": 9000,
        "pathMappings": {
            "/var/www": "${workspaceFolder}/www",
        },
        "log": true,
        "ignore": [
            "**/vendor/**/*.php"
        ]
    }]
}

应用程序正在运行,我在这一行将断点设置为www/public/index.php

$app = require_once __DIR__.'/../bootstrap/app.php';

并开始调试。我有一个调试控制台输出:

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

当我在浏览器中刷新应用程序时,我的 laravel 根目录中有两个新日志到文件 ./www/xdebug.log:

Log opened at 2019-01-13 13:29:35
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.17.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/public/index.php" language="PHP" xdebug:language_version="7.2.3" protocol_version="1.0" appid="6" idekey="user"><engine version="2.6.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

Log closed at 2019-01-13 13:29:36

Log opened at 2019-01-13 13:51:23
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.17.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/public/index.php" language="PHP" xdebug:language_version="7.2.3" protocol_version="1.0" appid="7" idekey="user"><engine version="2.6.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2018 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

Log closed at 2019-01-13 13:51:24

但调试器不会停止到我的断点。多次尝试更改xdebug参数和vscode配置,但无法解决问题。

请帮帮我,我的想法已经用完了......

【问题讨论】:

  • 使用另一个端口进行 Xdebug 通信(例如,9001 是一个典型的数字)。现在 Xdebug 可能正在连接到 php-fpm(默认情况下也使用 9000 端口)或错误的 IP/服务。完成后——用新的端口号更新你的问题中的配置——也许你也做错了其他事情。
  • 对不起,即使我换了端口,它也不起作用:我已经在 9001 使用了
  • 同样的设置有同样的问题;已经花了太多时间试图让它发挥作用。有人有解决办法吗?

标签: php docker visual-studio-code docker-compose xdebug


【解决方案1】:

我自己想通了这个,我想我会发布它以防它帮助其他人解决问题

据我了解,remote_host 应该设置为 'host.docker.internal'

我已经在我的 docker-compose 文件中设置了这个:

version: '3'
services:
  phpapp:
    image: phpapp
    container_name: phpapp
    environment: 
      XDEBUG_CONFIG: remote_host=host.docker.internal remote_port=9050 remote_enable=1 remote_autostart=1
    ports:
      - '8080:80'
    volumes:
      - 'c:/dev/www:/var/www/html'

【讨论】:

    【解决方案2】:

    自 xdebug 版本 3 以来,配置名称发生了重大变化。

    更改 dockerfile:

    RUN yes | pecl install xdebug \
        && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
        && echo 'xdebug.mode=debug' >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo 'xdebug.start_with_request=yes' >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo 'xdebug.client_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini \
        && echo 'xdebug.client_port=9050' >> /usr/local/etc/php/conf.d/xdebug.ini \
    

    在 vsc 配置"port": 9050,也更改端口

    顺便说一句,您不需要公开 9000 或 9050 端口

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2017-03-06
      • 2020-07-09
      • 2017-05-19
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多