Nic*_*mpe 2 windows xdebug docker visual-studio-code php-7.2
我正在尝试使用 VSCode 和 xDebug 设置 php 调试,但是 xDebug 无法连接到主机。因此,VSCode 也不会遇到任何断点。
当我在 VSCode 中启动调试侦听器,在 php-fpm 容器中运行 Bash shell 并尝试连接到主机时,它失败了:
$ docker-compose exec php-fpm bash
root@178ba0224b37:/application# nc -zv 172.20.0.1 9001
172.20.0.1: inverse host lookup failed: Unknown host
(UNKNOWN) [172.20.0.1] 9001 (?) : Connection refused
Run Code Online (Sandbox Code Playgroud)
我对 IP 地址感到困惑,因为在 Docker 设置中,虚拟交换机子网设置为10.0.75.0,而网络适配器vEthernet (DockerNAT)使用 IP 10.0.75.1。容器如何获取 IP 范围172.20.0.x?
在我的桌面上,我无法使用172.20.0.1.
它与 一起工作正常10.0.75.1,它phpinfo()按预期显示,但未触发断点。
phpinfo()显示 xDebug 已配置并且设置与我在配置中的设置相匹配php-ini-overrides.ini。
我禁用了防火墙,尝试了不同的 IP,并检查了端口和各种 xDebug、php、docker-compose 和 VSCode 调试设置。
我一直在四处寻找,但我想我仍然错过了一些东西。我的猜测是它与网络连接有关,但我不知道我还可以更改什么来解决此问题。
安装
主机是带有 docker-compose 和 VSCode 的 Windows 10。
我得到了搬运工debug-test从目录https://phpdocker.io/generator
基本上,它使用了两个码头工人的容器:nginx:alpine和phpdocker/php-fpm
我的 VSCode 工作区如下所示:

(自述文件来自 phpdocker.io 生成器并包含一些基本的 Docker 信息)
index.php 内容:
<?php
phpinfo(); // <-- VSCode breakpoint here
echo 'hello there';
?>
Run Code Online (Sandbox Code Playgroud)
容器的 IP 地址:
/debug-test-php-fpm - 172.20.0.3
/debug-test-webserver - 172.20.0.2
$_SERVER['REMOTE_ADDR']: 172.20.0.1 <- the host?
Run Code Online (Sandbox Code Playgroud)
配置和日志
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",
"pathMappings": {
"/application/public": "${workspaceRoot}/public"
},
"log": true,
"port": 9001,
"xdebugSettings": {
"max_data": 65535,
"show_hidden": 1,
"max_children": 100,
"max_depth": 5
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001
}
]
}
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml 内容:
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
webserver:
image: nginx:alpine
container_name: debug-test-webserver
working_dir: /application
volumes:
- .:/application
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
php-fpm:
build: phpdocker/php-fpm
container_name: debug-test-php-fpm
working_dir: /application
volumes:
- .:/application
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
Run Code Online (Sandbox Code Playgroud)
php-ini-overrides.ini 内容:
upload_max_filesize = 100M
post_max_size = 108M
# added for debugging with Docker and VSCode
xdebug.remote_enable=1
xdebug.remote_connect_back=On
xdebug.remote_autostart=1
# xdebug.remote_host=172.20.0.1 # using remote_connect_back instead, which should work for any IP
xdebug.remote_connect_back=1
xdebug.remote_port=9001
xdebug.profiler_enable=0
xdebug.var_display_max_depth = 5
xdebug.var_display_max_children = 256
xdebug.var_display_max_data = 1024
xdebug.remote_log = /application/xdebug.log
xdebug.idekey = VSCODE
Run Code Online (Sandbox Code Playgroud)
xdebug.log 访问页面后的内容:
Log opened at 2019-01-30 12:37:39
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.20.0.1:9001.
W: Creating socket for '172.20.0.1:9001', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2019-01-30 12:37:39
Log opened at 2019-01-30 12:37:39
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.20.0.1:9001.
W: Creating socket for '172.20.0.1:9001', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2019-01-30 12:37:39
Run Code Online (Sandbox Code Playgroud)
这不是粘贴错误,它实际上出于某种原因将请求记录了两次。
启动调试侦听器后在 VSCode 中调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?我迷路了.. 也许这与 DockerNAT 设置有关?
抱歉,帖子太长了。我还是 Docker 的新手,我希望这有所有需要的信息。
看我下面的回答。
经过一些编码后,我偶然发现了解决方案。
php 调试设置中的 IP 地址不正确。由于我的系统具有 VPN 连接、多个以太网适配器、多个虚拟交换机和多个虚拟机,因此找出在何处使用的内容有点棘手。
我在请求期间在 php 容器上运行 netstat 时偶然发现了 IP:
$ docker-compose ps --services
php
app
$ docker-compose exec php sh
/var/www/html # netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 08674b3fd785:58060 192.168.137.12:http TIME_WAIT
tcp 0 0 08674b3fd785:58062 192.168.137.12:http TIME_WAIT
[...]
udp 0 0 08874b3cd785:35298 192.168.65.1:domain ESTABLISHED
Run Code Online (Sandbox Code Playgroud)
我先尝试了192.168.65.1IP,但没有用。
这192.168.137.12是 php 脚本连接到的 Hyper-V 虚拟机的 IP。显然 php 容器可以连接到那个,所以也许它也可以连接到绑定到那个虚拟交换机的 Windows 适配器,换句话说:192.168.137.1.
添加此给Xdebug的设置,解决了这个问题:
xdebug.remote_host = 192.168.137.1。
| 归档时间: |
|
| 查看次数: |
3320 次 |
| 最近记录: |