How to get xdebug working with Docker for Mac

August 28, 2019

The problem with Docker for Mac and xDebug is that it is mapped to your localhost (127.0.0.1), so PHP/xDebug doesn’t actually know the true IP address of the remote host connecting to xdebug. To get around this what we need to do is configure an alias to the loopback device to get this work.

sudo ifconfig lo0 alias 10.254.254.254

Your xdebug configuration should then also include the following:

xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_host= 10.254.254.254

To persist the loopback address, you can create a plist, which will be loaded automatically when you boot your mac:

Filename: /Library/LaunchDaemons/com.ralphschindler.docker_10254_alias.plist

<?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.ralphschindler.docker_10254_alias</string>
    <key>ProgramArguments</key>
    <array>
        <string>ifconfig</string>
        <string>lo0</string>
        <string>alias</string>
        <string>10.254.254.254</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

To run this straightaway (without a reboot) run the following:

launchctl load /Library/LaunchDaemons/com.ralphschindler.docker_10254_alias.plist

Credit goes to @ralphschindler for this epic gist: Docker (Mac) De-facto Standard Host Address Alias