Docker xDebug 未连接到 VSCode
Docker xDebug not connecting to VSCode
我正在尝试使用 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
我对 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 调试设置。
我一直在广泛搜索,但我想我仍然遗漏了一些东西。我的猜测是它与网络连接有关,但我不知道我还能更改什么来解决此问题。
设置
主机是 Windows 10,具有 docker-compose 和 VSCode.
我从 https://phpdocker.io/generator
得到了 docker debug-test
目录
基本上它使用两个 docker 容器:nginx:alpine
和 phpdocker/php-fpm
我的 VSCode 工作区如下所示:
(自述文件来自 phpdocker.io 生成器并包含一些基本的 Docker 信息)
index.php
内容:
<?php
phpinfo(); // <-- VSCode breakpoint here
echo 'hello there';
?>
容器的 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?
配置和日志
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
}
]
}
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
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
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
这不是粘贴错误,它实际上出于某种原因记录了两次请求。
启动调试侦听器后 VSCode 中的调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
有什么想法吗?我迷路了..
可能与 DockerNAT 设置有关?
很抱歉 post。我对 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
我先尝试了 192.168.65.1
IP,但是没有用。
192.168.137.12
是 php 脚本连接到的 Hyper-V 虚拟机的 IP。显然 php 容器可以连接到它,所以也许它也可以连接到绑定到该虚拟交换机的 Windows 适配器,换句话说: 192.168.137.1
。
将此添加到 xDebug 设置解决了问题:
xdebug.remote_host = 192.168.137.1
.
我正在尝试使用 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
我对 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 调试设置。
我一直在广泛搜索,但我想我仍然遗漏了一些东西。我的猜测是它与网络连接有关,但我不知道我还能更改什么来解决此问题。
设置
主机是 Windows 10,具有 docker-compose 和 VSCode.
我从 https://phpdocker.io/generator
得到了 docker debug-test
目录
基本上它使用两个 docker 容器:nginx:alpine
和 phpdocker/php-fpm
我的 VSCode 工作区如下所示:
(自述文件来自 phpdocker.io 生成器并包含一些基本的 Docker 信息)
index.php
内容:
<?php
phpinfo(); // <-- VSCode breakpoint here
echo 'hello there';
?>
容器的 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?
配置和日志
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
}
]
}
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
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
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
这不是粘贴错误,它实际上出于某种原因记录了两次请求。
启动调试侦听器后 VSCode 中的调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
有什么想法吗?我迷路了.. 可能与 DockerNAT 设置有关?
很抱歉 post。我对 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
我先尝试了 192.168.65.1
IP,但是没有用。
192.168.137.12
是 php 脚本连接到的 Hyper-V 虚拟机的 IP。显然 php 容器可以连接到它,所以也许它也可以连接到绑定到该虚拟交换机的 Windows 适配器,换句话说: 192.168.137.1
。
将此添加到 xDebug 设置解决了问题:
xdebug.remote_host = 192.168.137.1
.