Skip to content

Instantly share code, notes, and snippets.

@manuelselbach
Last active February 19, 2024 11:41
Show Gist options
  • Save manuelselbach/8a214ae012964b1d49d9fb019f5f5d7b to your computer and use it in GitHub Desktop.
Save manuelselbach/8a214ae012964b1d49d9fb019f5f5d7b to your computer and use it in GitHub Desktop.
xdebug docker on macOS with PhpStorm

Use xdebug with docker on macOS and PhpStorm

To use xdebug with macOS and docker is quite, let´s call it tricky ;)

The following steps need to be proceed to get it working:

  1. use the config from the xdebug.ini wihtin your docker web container. Important: set remote_connect_back to off

UPDATE

As mentioned by some comments (thanks for the feedback), it is not needed to configure the IP manually. Thus, the next step is optional and the configuration (xdebug.ini) is updated to use the dynamic IP.

  1. optional: set up an alias for your local interface (lo)

To bring up the alias at startup, you can either (sudo may be needed here):

  • manually place the file com.manuelselbach.docker_10254254254_alias.plist into directory: /Library/LaunchDaemons/

  • Or use the script set_and_install_autorun_alias_for_lo.sh

Configure PhpStorm

After all that, just configure your PhpStorm:

  1. set port for xdebug Preferences -> Languages & Frameworks -> PHP -> Debug | Xdebug: Debug port = 9005
  2. configure a configuration in the toolbar
  • use PHP remote Debug
  • add a server to your domain (without protocoll like http:// or https://)
  • set port for http / https (not the xdebug port here)
  • select Debugger: Xdebug
  • if needed: set path mappings
  1. set Ide key (session id): to PHPSTORM

Happy debugging with docker!

<?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.manuelselbach.docker_10254254254_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>
#!/bin/bash
ifconfig lo0 alias 10.254.254.254
#!/bin/bash
ifconfig lo0 alias 10.254.254.254
curl https://gist.githubusercontent.com/manuelselbach/8a214ae012964b1d49d9fb019f5f5d7b/raw/fc57a5c8f13c6f9deb64d70f992a29487c49e494/com.manuelselbach.docker_10254254254_alias.plist >> /Library/LaunchDaemons/com.manuelselbach.docker_10254254254_alias.plist
# xdebug config within docker container
zend_extension=/path/to/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=off
xdebug.remote_host=host.docker.internal
# xdebug.remote_host=10.254.254.254 ## use this to configure it manually
xdebug.remote_port=9005
xdebug.idekey=PHPSTORM
xdebug.max_nesting_level=1500
@fabiojaniolima
Copy link

Should xdebug.remote_host be the ip address of the container?

@ojhaujjwal
Copy link

I have a suggestion. If you set xdebug.remote_host=docker.for.mac.localhost, you don't have to configure 10.254.254.254.
Check this discussion if you want to know why.

@sookoll
Copy link

sookoll commented Jan 31, 2019

It should work xdebug.remote_host=host.docker.internal
But I have found out, that it works occasionally. No idea why.

@wd3mo
Copy link

wd3mo commented Feb 22, 2019

Works for other IDEs too. For Visual Studio Code just change the xdebug.idekey=VSCODE and you are good to go.

@rsmondejar
Copy link

Thank you for all the information! It works like a charm.
xdebug.remote_host=docker.for.mac.localhost or xdebug.remote_host=host.docker.internal work fine for me.

@Sauerbrei
Copy link

Thank you.

@tixastronauta
Copy link

Important: set remote_connect_back to off

This saved my life 🙏

@romlly
Copy link

romlly commented Nov 20, 2019

I have a suggestion. If you set xdebug.remote_host=docker.for.mac.localhost, you don't have to configure 10.254.254.254.
Check this discussion if you want to know why.

Helped me a lot, thanks !

@oh-bala
Copy link

oh-bala commented Nov 27, 2019

Thanks, works like a charm and easy to setup, saved my time..

@neomobil
Copy link

neomobil commented Jan 4, 2020

Thank you!
It helped me too!

@lakhbawa
Copy link

Great Man, That Finally Worked, Actually For me 9005 was also not working, so had to change it to 9006

@axelvnk
Copy link

axelvnk commented Sep 22, 2021

For those of you installing xdebug 3 :) some settings were changed!

this stackoverflow helped me out : https://stackoverflow.com/questions/64776338/xdebug-3-the-setting-xdebug-remote-has-been-renamed-see-the-upgrading-g

@rizhk
Copy link

rizhk commented Oct 22, 2021

on mac, i used this setting :)
xdebug.remote_host=host.docker.internal
xdebug.remote_connect_back=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment