php - Docker容器上的PHP XDebug在VS Code中不起作用(Laravel和docker-compose)

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

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

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

我有这个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容器中:

enter image description here

这是我安装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调试扩展,并设置了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配置,但无法解决问题。

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

最佳答案

只是自己弄清楚了,以为我会发布它,以防其他人解决问题

据我了解,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'

关于php - Docker容器上的PHP XDebug在VS Code中不起作用(Laravel和docker-compose),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54169642/

相关文章:

python - 通过vscode的SSH隧道打开Matplotlib图

python - 在 VSCode 中使用 Flake8...?

php - 在 PHP 中连接到 SQL 数据库时抛出错误

php - 将 base64 编码的图像存储在数据库中是否明智?

docker - 在MacOS上使用docker作为提供程序的游民,无法在容器中挂载目录

docker - 对于 Gitlab CI,deploy.sh 文件是什么样的?

postgresql:dockerfile 从入口点脚本创建用户和数据库

git - 使用 VS Code 作为 git diff 工具

php - html 复选框遇到问题

php - 如何使用 jQuery 通过 Ajax 发送复选框数组的值?