CvRChameleon
CvRChameleon

Reputation: 421

Docker - Xdebug debug PHP CLI script (VS Code)

This question is regarding getting Xdebug to work with a CLI PHP script hosted inside a web-server Docker instance.

I have docker containers : web-server, varnish-cache, nginx-proxy. I am able to successfully debug a Magento 2 web-page via browser with this VS Code Launch config:

This is with the new XDebug v3 which removed alot of v2 configuration settings

Client (Windows 10) IP (my laptop) : 192.168.1.150, Host (Ubuntu 20.04) IP: 192.168.1.105, hosting with Docker containers IP: 172.100.0.2-5

VS Code launch:

 "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
            }
        },

XDebug configuration (PHP 7.3)

zend_extension=xdebug.so
xdebug.log=/var/log/apache2/xdebug.log
xdebug.idekey=VSCODE
xdebug.client_port=9099
xdebug.client_discovery_header=HTTP_X_REAL_IP
xdebug.discover_client_host=On
; fallback for CLI - use client_host
xdebug.client_host=172.100.0.2
xdebug.start_with_request=yes
xdebug.mode=debug

Docker network:

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": ""
            }

The CLI command: _as can be seen it's a Magento 2 bin/magento migrate:data command from within the hosting Apache2 Web-server Docker container. (IP shown above is then : 172.100.0.2)

rm var/migration* && bin/magento migrate:data /var/www/html/app/code/ModuleOverrides/Magento_DataMigrationTool/etc/opensource-to-opensource/1.7.0.2/config.localboth.host_restoredb.xml

No debug breakpoints will work in my VS Code on Windows 10 Client (IP 192.168.1.150) because I am calling the script from within the container 172.100.0.2.

The log file /var/log/apache2/xdebug.log confirms something along this line:

Could not connect to debugging client. Tried: 172.100.0.2:9099 (fallback through xdebug.client_host/xdebug.client_port) :-(

So, since I have no idea how to run a CLI script from Windows 10 client and only from within Docker container, how/what can I do to get this CLI script to connect to Xdebug?

Additional information (if needed)

Magento 2 has CLI capability bin/magento [command] - and the command I am trying to debug is part of the data-migration-tool which is failing to import attributes correctly. No-one has a 100% working solution on the github repo for this particular issue - so I want to try and dig deeper to try and find a solution. Also, the tool is only a CLI tool, no web-ui option.

Upvotes: 3

Views: 1983

Answers (1)

Derick
Derick

Reputation: 36784

You need to set Xdebug's xdebug.client_host to the IP address of your IDE, which you indicated is 192.168.1.150.

You also need to turn off xdebug.discover_client_host, as that would try to use the internal Docker network IP (172.100.0.2), which is not where your IDE is listening on.

Remember: Xdebug makes a connection to the IDE, not the other way around.

Upvotes: 5

Related Questions