【发布时间】:2019-12-13 08:56:01
【问题描述】:
我正在尝试对过去几个月运行良好的 Ubuntu 上的 Docker/PHP-Xdebug/VSCode 环境进行故障排除。现在 xdebug 无法连接到客户端。
使用docker exec --tty --interactive bw bash进入Docker后
php -v
返回
PHP 7.2.14 (cli) (built: Feb 6 2019 05:10:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
php -m -c 在 [Zend Modules] 部分列出 Xdebug。
这是 .ini 部分
[xdebug]
xdebug.default_enable = 1
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.idekey = VSCODE
xdebug.remote_handler = "dbgp"
xdebug.remote_port = 9000
xdebug.remote_log=/var/log/apache2/xdebug.log
xdebug.remote_timeout = 500
其中的所有内容都会相应地反映在phpinfo() 输出中。
docker-compose.yml
version: "3.1"
services:
webserver:
build: docker/apache
container_name: bw
volumes:
- .:/var/www/html
ports:
- "8000:80"
expose:
- "9000"
restart: always
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log":true,
"pathMappings": {
"/var/www/html": "${workspaceRoot}/",
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
} ]
}
这是 xdebug.log 的尾部
[156] Log opened at 2019-08-05 19:39:58
[156] I: Checking remote connect back address.
[156] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[156] I: Checking header 'REMOTE_ADDR'.
[156] I: Remote address found, connecting to 172.18.0.1:9000.
[156] E: Time-out connecting to client (Waited: 500 ms). :-(
[156] Log closed at 2019-08-05 19:39:59
我强烈怀疑我缺乏 Docker 知识是让这项工作再次发挥作用的问题。
任何帮助将不胜感激。
【问题讨论】:
-
1) IP 错误?它必须是运行调试客户端 (VSCode) 的计算机的 IP,从 PHP/webserver 容器可以看出。尝试禁用
xdebug.remote_connect_back = 0并手动指定IP 地址(Docker 有特殊的主机名) 2) 防火墙?确保您可以在 VSCode 侦听传入的 Xdebug 连接时连接到它(使用telnet或类似方法对其进行测试) 3) 您的 Web 服务器是什么以及如何在那里运行 PHP? Apache 模块 .. 还是 php-fpm? -
4) 为什么需要在 docker-compose.yml 中公开“9000”?暴露意味着到该端口的连接将从主机转发到容器(因此端口很忙)......而应该监听它的是VSCode..如果其他东西已经占用了该端口,那么VSCode可能无法使用它...
-
谢谢您,先生。原来这是一个防火墙问题,我感到很愚蠢,因为你让我意识到我在排除故障时使用了相反的 telnet 命令(从主机到容器)。当我将
sudo ufw allow from any to any port 9000 proto tcp应用到主机时,一切都恢复了。我不知道为什么该规则从防火墙中消失了(或者它是否存在但更严格),因为它在几周前还在工作,我希望我的防火墙日志能够及时返回。您想发布答案以便我接受还是应该接受? -
自己做(你可以接受自己的答案)。
标签: php docker visual-studio-code xdebug