在 Docker for Mac 上使用 SSH 隧道进行 Xdebug

Posted

技术标签:

【中文标题】在 Docker for Mac 上使用 SSH 隧道进行 Xdebug【英文标题】:Xdebug with SSH tunnel on Docker for Mac 【发布时间】:2017-04-01 15:43:51 【问题描述】:

我最近从 Docker 社区阅读了很多关于如何在 phpStorm 中使用 Docker for Mac 调试 PHP 应用程序的帖子。所有这些都包含一些有用的信息,但还没有在一个地方看到有效的解决方案。

【问题讨论】:

是问题还是答案? 转换为问答式 所以接受答案是有意义的,尼特?这是一个很好的,我相信你的努力将不胜感激。我只是说这种habra 风格的文章更适合***.com/documentation。对不起,如果我之前的评论有点误导。 【参考方案1】:

这对我有用。

Docker 容器内部

编辑 xdebug 配置

# automatically start debugger on every request
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port=9000
# send all debug requests to 127.0.0.1, remote_connect_back should be turned off
xdebug.remote_connect_back = 0
xdebug.remote_host=127.0.0.1

#log all xdebug requests to see is it working correctly
xdebug.remote_log=/var/log/remote.log

验证 xdebug 是否有效

此时尝试运行 PHP 应用程序。日志应包含每个请求的此类条目:

I: Connecting to configured address/port: 127.0.0.1:9000 I: Connected to client. :-)

如果您在日志中看到类似的内容,说明 remote_host 或 remote_connect_back 配置错误。

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.18.0.1:9000. W: Creating socket for '172.18.0.1:9000', poll: Operation now in progress. E: Could not connect to client. :-(

我见过 Xdebug 在 CLI 中运行但不能从浏览器运行的情况,当这个问题出现在日志中时,remote_connect_back=0 修复了它。

sshd 配置

为了允许 ssh 隧道连接到容器:编辑 /etc/ssh/sshd_conf 并添加:

GatewayPorts yes

如果需要,重新启动 sshd(理想情况下,这应该是 Dockerfile 的一部分)。

在主机上

启动反向 SSH 隧道

运行此命令并将其保持在单独的终端选项卡中打开: ssh -p container_22_port -R 9000:localhost:1111 root@127.0.0.1

其中 container_22_port 是主机上的端口映射到 docker 容器上暴露的 22 端口。 9000是Xdebug在容器内使用的端口,1111是主机用来监听Xdebug连接的端口。

用 netcat 测试

此时,您可以验证 Xdebug 是否确实将信息从 docker 容器内部传递到主机。启动netcat查看发送到1111端口的内容并运行php应用:

nc -l 1111

您应该会看到如下内容:

<?xml version="1.0" encoding="iso-8859-1"?>
<init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/magento2/index.php" language="PHP" xdebug:language_version="7.0.12" protocol_version="1.0" appid="1006" idekey="XDEBUG_ECLIPSE"><engine version="2.5.0rc1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2016 by Derick Rethans]]></copyright></init>

配置 PhpStorm

打开File->DefaultSettings,然后在Languages&amp;Frameworks->PHP->DebugXdebug->Debug port改成1111(我们用来打开ssh的那个)隧道)。 PhpStorm 此时应该开始接受来自 xdebug 的连接。

这种方法有什么问题吗?

【讨论】:

如果这不是某个地方的文档,您绝对应该考虑将其贡献给php debugging 和/或xdebug 的社区文档,这真的写得很好 我喜欢这个解决方案,我对 container_22_port 感到困惑。【参考方案2】:

我发现您不需要 SSH 隧道即可使其工作。

Xdebug 只需要连接到正在运行的 IDE 调试器,但可能有一些默认端口已经使用,例如用于 FPM (9000) 的端口。

容器内

设置 xdebug 选项如下:

xdebug.remote_enable = 1
xdebug.remote_host = localhost
xdebug.remote_port = 10000
xdebug.remote_connect_back = 1

注意:如果使用 nginx-proxy 容器作为反向代理,请将您的 remote_host 设置为您在 VIRTUAL_HOST 中定义的那个

使用 PhpStorm

文件 -> 设置 语言和框架 PHP 服务器:添加一个并将主机设置为 localhost:80 并选择 Xdebug 并选择 路径映射 ...将 Docker 内的文件夹 (/var/www/) 映射到一个 调试:将 Xdebug 端口 设置为 10000,并选中 可以接受外部连接

【讨论】:

【参考方案3】:

使用xdebug.remote_host=host.docker.internal 可以在任何地方使用。

【讨论】:

以上是关于在 Docker for Mac 上使用 SSH 隧道进行 Xdebug的主要内容,如果未能解决你的问题,请参考以下文章