How to get xdebug working with Docker for Mac

Published:

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.

1
sudo ifconfig lo0 alias 10.254.254.254

Your xdebug configuration should then also include the following:

1
2
3
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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:

1
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

Looking for an expert Magento 2 Developer to help with your project?

Contact Me Today
Show Comments