Docker-Xdebug调试PHP CLI脚本(VS代码)

Docker-Xdebug调试PHP CLI脚本(VS代码),docker,debugging,visual-studio-code,magento2,xdebug,Docker,Debugging,Visual Studio Code,Magento2,Xdebug,这个问题是关于让Xdebug使用web服务器Docker实例中托管的CLI PHP脚本的 我有docker容器:web服务器,varnish缓存,nginx代理。 我能够使用以下VS代码启动配置通过浏览器成功调试Magento 2网页: 新的XDebug v3删除了许多v2配置设置 客户端(Windows 10)IP(我的笔记本电脑):192.168.1.150,主机(Ubuntu 20.04)IP:192.168.1.105,使用Docker容器托管IP:172.100.0.2-5 VS代码启

这个问题是关于让Xdebug使用web服务器Docker实例中托管的CLI PHP脚本的

我有docker容器:
web服务器
varnish缓存
nginx代理
。 我能够使用以下VS代码启动配置通过浏览器成功调试Magento 2网页:

新的XDebug v3删除了许多v2配置设置

客户端(Windows 10)IP(我的笔记本电脑):192.168.1.150,主机(Ubuntu 20.04)IP:192.168.1.105,使用Docker容器托管IP:
172.100.0.2-5

VS代码启动:

 "name": "(Magento 2) Listen for XDebug on 192.168.1.5/105",
            "type": "php",
            "request": "launch",
            "port": 9099,
            "stopOnEntry": false, // Set to true to test any script on entry
            "log": false,
            // Remember to update remote_connect_back or remote_host
            // inside xdebug PHP configuration.
            // When using CLI debugging - rather use remote_host,
            // because remote_connect_back = 1 does not work with CLI
            // Server -> Local
            "pathMappings": {
                "/var/www/html/": "${workspaceRoot}",
            },
            "xdebugSettings": {
                "max_children": 10000,
                "max_data": 10000,
                "show_hidden": 1
            }
        },
docker inspect network magento2-network-frontend:
        "Containers": {
            "6538a93fbe811fbbd9646d4ce089e1b686b508862ed86f6afaac1b600043a1e5": {
                "Name": "redis-cache-magento2.3.5",
                "EndpointID": "d27bfbff61765cf2b840e98d43ec7a378e182baa7007dabde4bab5a41734fa2a",
                "MacAddress": "02:42:ac:64:00:05",
                "IPv4Address": "172.100.0.5/16",
                "IPv6Address": ""
            },
            "7c7ba745db17d6d6a100901ed1e3fe38a3d26a97e086edc155254a7d41033bcf": {
                "Name": "web-server-apache2-magento2-3-5",
                "EndpointID": "9b81f6b7ff2292eba6fb68af209f1d5c958bea3ee0d505512862f225ed8e57be",
                "MacAddress": "02:42:ac:64:00:02",
                "IPv4Address": "172.100.0.2/16",
                "IPv6Address": ""
            },
            "7f208ecce2aafdf182e4616ef2e8b043f3b8245018c299aae06c1acf4fc0d029": {
                "Name": "varnish-cache-magento2-3-5",
                "EndpointID": "e1c4e3f9e792b7dfd2cebfbb906bd237795820639a80ab8f530f0c8418257611",
                "MacAddress": "02:42:ac:64:00:03",
                "IPv4Address": "172.100.0.3/16",
                "IPv6Address": ""
            },
            "dc599fa93b09650b70f8f95333caecc8f9db18cd19b17be57d84196e91f54c2a": {
                "Name": "nginx-proxy-magento2-3-5",
                "EndpointID": "7b8396af676d9af51b098d09f20d9e73ef83f4b085cb5f7195ea234aae7ed91d",
                "MacAddress": "02:42:ac:64:00:04",
                "IPv4Address": "172.100.0.4/16",
                "IPv6Address": ""
            }
XDebug配置(PHP7.3)

码头工人网络:

 "name": "(Magento 2) Listen for XDebug on 192.168.1.5/105",
            "type": "php",
            "request": "launch",
            "port": 9099,
            "stopOnEntry": false, // Set to true to test any script on entry
            "log": false,
            // Remember to update remote_connect_back or remote_host
            // inside xdebug PHP configuration.
            // When using CLI debugging - rather use remote_host,
            // because remote_connect_back = 1 does not work with CLI
            // Server -> Local
            "pathMappings": {
                "/var/www/html/": "${workspaceRoot}",
            },
            "xdebugSettings": {
                "max_children": 10000,
                "max_data": 10000,
                "show_hidden": 1
            }
        },
docker inspect network magento2-network-frontend:
        "Containers": {
            "6538a93fbe811fbbd9646d4ce089e1b686b508862ed86f6afaac1b600043a1e5": {
                "Name": "redis-cache-magento2.3.5",
                "EndpointID": "d27bfbff61765cf2b840e98d43ec7a378e182baa7007dabde4bab5a41734fa2a",
                "MacAddress": "02:42:ac:64:00:05",
                "IPv4Address": "172.100.0.5/16",
                "IPv6Address": ""
            },
            "7c7ba745db17d6d6a100901ed1e3fe38a3d26a97e086edc155254a7d41033bcf": {
                "Name": "web-server-apache2-magento2-3-5",
                "EndpointID": "9b81f6b7ff2292eba6fb68af209f1d5c958bea3ee0d505512862f225ed8e57be",
                "MacAddress": "02:42:ac:64:00:02",
                "IPv4Address": "172.100.0.2/16",
                "IPv6Address": ""
            },
            "7f208ecce2aafdf182e4616ef2e8b043f3b8245018c299aae06c1acf4fc0d029": {
                "Name": "varnish-cache-magento2-3-5",
                "EndpointID": "e1c4e3f9e792b7dfd2cebfbb906bd237795820639a80ab8f530f0c8418257611",
                "MacAddress": "02:42:ac:64:00:03",
                "IPv4Address": "172.100.0.3/16",
                "IPv6Address": ""
            },
            "dc599fa93b09650b70f8f95333caecc8f9db18cd19b17be57d84196e91f54c2a": {
                "Name": "nginx-proxy-magento2-3-5",
                "EndpointID": "7b8396af676d9af51b098d09f20d9e73ef83f4b085cb5f7195ea234aae7ed91d",
                "MacAddress": "02:42:ac:64:00:04",
                "IPv4Address": "172.100.0.4/16",
                "IPv6Address": ""
            }
CLI命令:\可以看出,它是托管Apache2 Web服务器Docker容器中的Magento 2
bin/Magento migrate:data
命令。(上面显示的IP为:
172.100.0.2

Windows 10客户端(IP 192.168.1.150)上的VS代码中没有调试断点,因为我正在容器中调用脚本172.100.0.2

日志文件
/var/log/apache2/xdebug.log
确认了以下内容:

Could not connect to debugging client. Tried: 172.100.0.2:9099 (fallback through xdebug.client_host/xdebug.client_port) :-(
因此,由于我不知道如何从Windows 10客户端运行CLI脚本,并且只能从Docker容器中运行,因此如何/如何才能使此CLI脚本连接到Xdebug

其他信息(如果需要)


Magento 2具有CLI功能
bin/Magento[command]
——我尝试调试的命令是
数据迁移工具的一部分,该工具无法正确导入属性。对于这个特定的问题,没有人能在github回购协议上找到100%的有效解决方案——因此我想尝试并深入挖掘,以尝试找到一个解决方案。此外,该工具仅是一个CLI工具,没有web ui选项

您需要将Xdebug的
Xdebug.client\u主机
设置为IDE的IP地址,您指出的IP地址为
192.168.1.150

您还需要关闭
xdebug.discover\u client\u host
,因为这样会尝试使用内部Docker网络IP(
172.100.0.2
),而IDE不在该IP上侦听


记住:Xdebug连接到IDE,而不是相反。

如果需要其他信息,请告诉我。我现在真的很想解决这个问题,但也是因为我将来需要调试CLI脚本。我从未想过这是一个如此不可能的请求,成千上万的PHP开发人员,没有人能够回答如何让xdebug使用Docker的CLI脚本。请大家:)这里有人请帮忙!嗯,我刚刚发现我一直在关注错误的事情。。。我不知道xdebug会将连接从
172.100.0.2:9099
(这是xdebug继续抱怨无法连接到
的docker连接)转发到我的IDE
192.168.1.150:9099
。为他人提供重要信息_xdebug的错误输出让我很困惑,我认为应该让xdebug连接在
172.100.0.2:9099
下工作,但结果证明您的解决方案是有效的。如果可以,我将为您的答案添加其他信息,这可能会对其他人有所帮助。我为答案添加了其他重要信息(还有其他需要考虑的事项)。我已经更新了答案,希望它能有所帮助。是的,xdebug日志文件中的错误信息让我产生了相反的想法。我只意识到你的答案正好相反。。。。因为我必须弄明白为什么你的答案会起作用。再次感谢!我的编辑没有被接受,我想是因为这是你的赏金,系统不允许我添加信息。这可能只与Magento相关,但其他人可能会发现这对故障排除很有用。您可以添加运行
php-bin/magento-some:command
而不是
bin/magento-some:command
\n的事实吗?即使
bin/magento
是一个php脚本,在不调试CLI时也可以正常运行。如果没有在命令前面显式调用
php
,断点在将Docker连接到我的IDE的CLI上不起作用。