Docker and XDebug not reading breakpoints VSCode

I have been using XDebug with PHP Version 7.0.* for the last 6 months on a MAC using remote debugging to a Docker container.

I was running an older version of docker that was using VirtualBox to VM for docker, all was working fine.

I recently updated docker to version 17.03.1 and have had nothing but issues with xDebug. I have contacted the creator of vscode-php-debug via an issue on his repository and he then pointed me to take the issue up with xdebug and or docker.

My issue is the following:

XDebug is running and working on my container, the xdebug log shows that it connects to my IDE but it simply closes the connection as if there are no breakpoints when I have breakpoints set in VSCode.

The issue I posted on vscode-php-debug can be found here

Which has a copy of my xDebug logs and VSCode debug logs... It shows that the connection is made but no breakpoints are hit.

I have read around a few blogs that docker now needs a loopback ip created for the connection to xdebug to work, which I have also tried and failed.

I'm finding it surprisingly hard to debug without a debugger after using one for so long.

I have tried many configs, reinstalled, rebooted, rebuilt images/containers and even have tried the docker and xdebug irc channels on freenode without any success.


Solution 1:

EDIT-2 2018

The remote_host value can now be changed to support all platforms:

xdebug.remote_host = "host.docker.internal"

EDIT-1 2018 It's no longer needed to use the plist fix. As pointed out in this gist: https://gist.github.com/chadrien/c90927ec2d160ffea9c4#gistcomment-2398281 you can now use the docker for mac internal IP.

[xdebug]
xdebug.remote_host = "docker.for.mac.host.internal"
xdebug.default_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 0
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_port = 9000
xdebug.idekey="PHPSTORM"

OLD CONFIG

Since you are using docker on a mac I'm posting the way my solution worked. Most of the credits go to this post on the docker forum.

Assuming your installation of xdebug is correctly, this is my config in the php.ini.

[xdebug]
xdebug.remote_host=10.254.254.254
xdebug.remote_autostart=1
xdebug.idekey = PHPSTORM
xdebug.default_enable = 0
xdebug.remote_enable = 1
xdebug.remote_connect_back = 0
xdebug.profiler_enable = 1

You can test your config by executing this command in your terminal. sudo ifconfig en0 alias 10.254.254.254 255.255.255.0.

If this is working you can convert it into a plist file and place it in the following location. /Library/LaunchDaemons/com.docker.xdebugFix.plist. Below you will find my version of the plist file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.docker.xdebugFix</string>
    <key>ProgramArguments</key>
    <array>
        <string>ifconfig</string>
        <string>en0</string>
        <string>alias</string>
        <string>10.254.254.254</string>
        <string>255.255.255.0</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Note: The plist will only work after a reboot of your Mac.


PHPSTORM Config (also needed after the 2018 edit)

After that I set up my PHP storm with a debug server like this: php storm config 1

php storm config 2

After that my breakpoints where working, if you're using chrome you'll also need to use the xdebug extension but I'm pretty sure you know this since you used it in the past.