我无法使用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调试扩展,并设置了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/