我需要在远程服务器上的Docker容器中调试PHP脚本。
通常情况下,我会使用Windows中的Visual Studio Code来调试我的代码。我一直以来都是用这种方式来调试我的开发代码(位于Vagrant中),没有遇到过任何问题。
但是当我要调试远程服务器上的Docker中的文件时,我就无法使其正常工作。
以下是我的xdebug设置,位于php.ini文件中(在远程服务器的docker中)。
通常情况下,我会使用Windows中的Visual Studio Code来调试我的代码。我一直以来都是用这种方式来调试我的开发代码(位于Vagrant中),没有遇到过任何问题。
但是当我要调试远程服务器上的Docker中的文件时,我就无法使其正常工作。
以下是我的xdebug设置,位于php.ini文件中(在远程服务器的docker中)。
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host=172.18.0.1
xdebug.remote_port=9000
xdebug.remote_log=/tmp/debug.log
上述设置中的remote_host
IP地址是从Docker内部看到的远程主机的IP。
我尝试使用ssh隧道连接到我的远程服务器(在Windows上使用telnet)。
但当我尝试在浏览器上运行该网站时,我的Visual Studio Code无法检测到调试会话。
基本上我所做的是这些:
- 在我的本地机器(Windows)上启动调试会话
- 使用
ssh -R 9000:localhost:9000 username@myremoteserver.com
创建到远程服务器的ssh隧道(将服务器上的端口9000转发到localhost:9000) - 将Docker上的9000端口映射到远程服务器上的9000端口
- Docker中的Xdebug设置为监听其主机(因此要使用从Docker内部看到的主机IP地址,可以通过在Docker内部运行此命令获取:
/sbin/ip route|awk '/default/ { print $3 }'
当我运行该网站时,/tmp/debug.log
包含以下内容:
I: Connecting to configured address/port: 172.18.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///magento/test.php" language="PHP" xdebug:language_version="5.6.33-1+ubuntu16.04.1+deb.sury.org+1" protocol_version="1.0" appid="12776" idekey="www-data"><engine version="2.5.5"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2017 by Derick Rethans]]></copyright></init>
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>
我该如何将由Xdebug在Docker中设置的连接“转发”到我的远程服务器,再到我本地pc上的IDE?
172.18.0.1
来自哪里--对于SSH隧道,您应该真正使用127.0.0.1
。 - LazyOne