3

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.

2
  • Please tell me if there is any other information that is required. I would really like to get this resolved, for now but also because I would need to debug CLI scripts in the future. Commented Mar 24, 2021 at 8:06
  • I never thought this is such an impossible request, thousands of PHP developers, and no-one can answer how to get xdebug to work with a CLI script with Docker. Please guys :) someone here please assist! Commented Mar 26, 2021 at 7:05

1 Answer 1

5
+50

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, I just discovered that I have been concentrating on the wrong thing... I didn't understand that xdebug will forward the the connection from 172.100.0.2:9099 (which was the docker connection the xdebug continued to complain about not being able to connect to...) to my IDE 192.168.1.150:9099. Important information for others. _The error output of xdebug confused me and thought I should make the xdebug connection work with 172.100.0.2:9099 but it turns out your solution worked. If ok, I will add additional information to your answer that might help others.
I added additional important information to the answer (there were additional things to consider) in conjunction to your additional answer.
Yes, the error information from xdebug log files had me thinking its the other way round. I only realized the inverse on your answer.... because I had to make sure to see why your answer should work. Thanks again!
My edits were not accepted, I assume because it was your bounty, the system does not allow for me to add info. This might only be related to Magento specifically, but others might find this useful for troubleshooting. Can you please add the fact to run php bin/magento some:command and not bin/magento some:command _even though bin/magento is a PHP script and runs fine just like that when not debugging CLI. Breakpoints didnt work on CLI with Docker to my IDE without explicitly calling php in front of command.
I think that's a Magento specific thing? It's possible it starts PHP without an ini file or something like that? It's certainly not something that Xdebug does care about.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.