Xdebug与Docker for Mac上的SSH隧道

我最近从Docker社区上阅读了很多关于如何使用Docker for MacdebuggingPHPStorm中的PHP应用程序的文章。 他们都包含有用的信息,但没有在一个地方看到工作的解决scheme。

这是什么为我工作。

里面的docker集装箱

编辑xdebugconfiguration

# 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 

validationxdebug的工作

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

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

如果在日志中看到类似这样的内容,则remote_host或remote_connect_backconfiguration不正确。

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修复了它。

sshdconfiguration

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

GatewayPorts yes

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

在主机上

开始反向SSH隧道

运行这个命令,并将其保存在单独的“terminal”选项卡中: ssh -p {container_22_port} -R 9000:localhost:1111 root@127.0.0.1

其中{container_22_port}是主机上映射到docker容器上的exdposed 22端口的端口。 9000是Xdebug内部容器使用的端口,1111端口将被主机用来侦听Xdebug连接。

用netcattesting

在这一点上,您可以validationXdebug是否实际上将信息从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> 

configurationPhpStorm

Opne File – > DefaultSettings ,并在那里在Languages&Frameworks – > PHP – > Debug更改Xdebug – > Debug port1111 (我们用来打开SSH隧道)。 PhpStorm现在应该开始接受来自xdebug的连接。

这种方法有什么担忧吗?

我发现你不需要一个SSH隧道来使它工作。

Xdebug只需连接到正在运行的IDEdebugging器,但可能会有一些默认端口已经用于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与path映射select…映射文件夹内的Docker(/ var / www /)到一个
        • debugging:选中可以接受外部连接,Xdebug端口设置为10000