在Docker for Mac上使用SSH隧道的Xdebug

Eug*_*ika 1 php xdebug ssh-tunnel docker docker-for-mac

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

Eug*_*ika 5

这对我有用.

在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
Run Code Online (Sandbox Code Playgroud)

验证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容器上的exdposed 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>
Run Code Online (Sandbox Code Playgroud)

配置PhpStorm

Opne File- > DefaultSettings,然后在Languages&Frameworks- > PHP- > Debug更改Xdebug- > Debug port1111(我们用来打开ssh隧道的那个).PhpStorm此时应该开始接受来自xdebug的连接.

这种方法有什么问题吗?