IP address for xdebug

That’s how I do it:

  • sudo ifconfig lo0 alias 10.254.254.254 (create an alias for your mac loopback interface)
  • xdebug.remote_host=10.254.254.254 in your xdebug.ini
  • configure your ide as usual.

I’ve been using this through several beta versions, never had a problem. Beware of port 9000 conflicts.

A startup script to standardize on using 10.254.254.254:

3 Likes

Are you using docker for mac or toolbox? Those settings work for me if I use toolbox, but don’t work on docker for mac.

You can use 172.0.0.1 as xdebug.remote_host in the xdebug.ini file

still broken in 1.12.1-beta24 . setting a remote host works, but i think it is important to use remote connect back, so this will work on different environments like toolbox-mac, docker mac, linux …

I tried doing this… but then what IP/interface do you listen on inside the IDE? (In this case, PhpStorm)

Man I have looked through so many Docker forum and StackOverflow posts but can’t seem to get it to work. Does somebody mind looking at our setup and finding the problem? https://gist.github.com/Toosick/4639f4e33ad52c692876a89df89e2b40

Try IP from here as xdebug.remote_host : cat /Users/<YOUR_USERNAME>/Library/Containers/com.docker.docker/Data/database/com.docker.driver.amd64-linux/slirp/host

This used to work for me using the Docker for Mac beta, but after updating to v1.12.1 stable, the mentioned file no longer exists…

Is there any way to get this IP without setting up a loopback IP? If not, there should be.

I used to use the loopback alias. It works great but if you don’t have any network connection (no wifi), the connection between xdebug and the host can’t be established.

Has someone an idea on how to resolve this ? and ensure that the connection will work even if you work offline ?

Thanks,

Instead of creating loopback workaround, you can also use ssh tunnel with this configuration:
xdebug.remote_connect_back = 0
xdebug.remote_host=127.0.0.1
Posted full solution here http://stackoverflow.com/questions/40666083/xdebug-with-ssh-tunnel-on-docker-for-mac

After much pain I got this working with the following:
Dockerfile

RUN apt-get update && apt-get upgrade -y && apt-get autoremove -y \
    && apt-get install -y git curl \
    && pecl install xdebug \
    && rm -rf /tmp/pear \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)\n"
       >> /usr/local/etc/php/conf.d/xdebug.ini

PHP.ini

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

sudo ifconfig en0 alias 10.254.254.254 255.255.255.0

I wrote up and recorded my workflow here: Debugging PHP apps

Hope this helps someone.

On a windows host, I got this to work by using the IPv4 address used by the vEthernet (DockerNAT) connection. Just check your network connections, view that specific connection’s details to get the ip address and plug that into your xdebug.remote_host. I also had to enable remote_autostart (for some reason, I could never get xdebug to trigger using GET/POST/cookies)

1 Like

Thank you for suggestion about DockerNAT address on Windows 10 host. Also, for me following configuration was enough:
xdebug.remote_host=10.0.75.1
xdebug.idekey=PHPSTORM
xdebug.remote_enable=1
xdebug.remote_autostart=0

I wrote that for our company php image, it can be useful for someone: https://github.com/jorge07/alpine-php/blob/master/doc/IDE.md

Alright, I’d like to add my two cents to this excellent thread.

So, normally, you could set xdebug.remote_connect_back=on in order for Xdebug to connect to any client that makes a request to the server regardless of hostname/IP. When this is on, the xdebug.remote_host setting is ignored.

For whatever reason, Docker for Mac prevents Xdebug from allowing such unrestricted connections. One reason could be because when you enable xdebug.remote_connect it tries to connect to 127.0.0.1:9000, which is the IP of the request according to the container, probably due to how Docker for Mac integrates the networking of the host and container. As a result, it thinks that the request is coming from the container rather than the host. I’m not totally sure if this is what’s happening, but it’s my best guess.

I, like pretty much everyone else here, could not get Xdebug to work with such a Docker for Mac container until I disabled xdebug.remote_connect_back.

It would appear that you can set xdebug.remote_host to any hostname/IP that your host computer is identified by, not the IP that the container uses to identify your host, which typically starts with 172. You can set an alias IP on your host if you want, but it would appear that you can likewise use an IP already assigned to the host on the local network. In fact, Docker has a brief Xdebug tutorial where they do this. I was confused by their instructions to use the host’s private IP because in other containers, like Vagrant and Docker via VirtualBox, you do have to set xdebug.remote_host to the IP that the container identifies the host with, which is typically the the gateway IP of the container, or the one seen in the access logs.

And so, my working Xdebug config on a Docker for Mac container is simply the following:

xdebug.remote_enable=on
xdebug.remote_host=10.0.0.5 # My host's IP on en1
xdebug.remote_port=9001 # 9000 is already in use on my host and therefore unavailable for the container
1 Like

Thanks for this excellent thread - I’ve been struggling with with the same XDebug problem with mac and PHPStorm and stumbled on to a slightly better way to setup your php.ini if you’re using Docker For Mac 17.06+:

xdebug.remote_enable=1
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_connect_back=0

At least this eliminates a static IP which is annoying if you’re on DHCP. I’m working on a new image that will hopefully be able to do a detection if it’s Docker For Mac or not and setup the php.ini this way if so, otherwise use remote_connect_back.

2 Likes

Thank you! Work perfect :slight_smile:

This really helped me! Specifically the xdebug.remote_host=docker.for.mac.localhost part. Any ideas what this means?

1 Like

nice, it’s works.

Here are some general instructions regardless of Docker on Linux, Docker for Windows, Docker for Mac or Docker Toolbox:

https://devilbox.readthedocs.io/en/latest/intermediate/configure-php-xdebug.html