Magento, Docker & Xdebug
As I have written before, we are big fans of Mark Shust's Docker Configuration for Magento project. Even though we customized the setup a bit for our needs, it comes with a lot of good stuff out of the box, like Xdebug support.
I had the need to debug an import issue we had with running TechDivision's Pacemarker import tool for one of our merchants. Since we run Pacemaker locally in the cron container that ships with Mark's setup, I configured Xdebug for the cron container in the same manner as it is confirmed for the phpfpm container by default and started a debugging session in PhpStorm. I quickly realized that this is was not the best idea as the different cron jobs running in the background opened one debug connection after another making it impossible to debug properly. So I changed the plan and tried to debug the issue within the phpfpm container. I made sure that Xdebug won't automatically reconnect back for the web requests to avoid similar issues and started the m2if import via the CLI like this:
PHP_IDE_CONFIG="serverName=myserver.loc" \ XDEBUG_CONFIG="remote_connect_back=1 remote_host=172.26.0.1" \ php /var/www/html/vendor/bin/import-m2if import:products add-update \ --archive-artefacts=false --serial="11" \ --pid-filename="/var/www/html/var/tmp/aff4fb9432bf030cddffa1061c74.pid" \ --source-dir="/var/www/html/var/pacemaker/pipelines" \ --target-dir="/var/www/html/var/pacemaker/pipelines" \ --magento-version="2.3.5-p1" \ --installation-dir="/var/www/html" \ --magento-edition="CE" \ --configuration=/var/www/html/app/code/Vendor/Module/etc/product.json
Basically it boils down to setting the two environment variables PHP_IDE_CONFIG and XDEBUG_CONFIG before running the php script. PHP_IDE_CONFIG will tell PhpStorm which server configuration to use to make sure the path mapping matches. The XDEBUG_CONFIG settings instruct Xdebug to instantly connect back to the remote host - my docker host IP in this case.