Home > Back-end >  Xdebug inside Docker on Mac M1 2021 is not working
Xdebug inside Docker on Mac M1 2021 is not working

Time:03-16

I ported a local development setup from Linux to a new Mac machine and am having problems with getting Xdebug to work with PhpStorm in a Mac M1 Pro machine.

I have one container with PHP where Xdebug is installed and configured like this:

zend_extension=/usr/lib/php8/modules/xdebug.so
xdebug.mode=debug

xdebug.idekey=PHPSTORM
xdebug.client_port=9001
xdebug.start_with_request=yes
xdebug.discover_client_host=1
xdebug.log=/var/log/xdebug.log

In PhpStorm I am listening to port 9001 (I use 9000 for other service).

Here are Xdebug logs inside docker container at /var/log/xdebug.log

/var/www/html/app # tail -f /var/log/xdebug.log 
[49] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[49] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
[49] [Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.18.0.4:9001.
[49] [Step Debug] WARN: Creating socket for '172.18.0.4:9001', poll success, but error: Operation in progress (29).
[49] [Step Debug] WARN: Could not connect to client host discovered through HTTP headers, connecting to configured address/port: localhost:9001. :-|
[49] [Step Debug] WARN: Creating socket for 'localhost:9001', poll success, but error: Operation in progress (29).
[49] [Step Debug] WARN: Creating socket for 'localhost:9001', connect: Address not available.
[49] [Step Debug] ERR: Could not connect to debugging client. Tried: 172.18.0.4:9001 (from REMOTE_ADDR HTTP header), localhost:9001 (fallback through xdebug.client_host/xdebug.client_port) :-(
[49] Log closed at 2022-03-15 16:28:46.957669

I don't understand why the connection is not happening/failing.

Also I have a docker-compose.yml file that has the service API above as

  api:
    build:
      context: .
    expose:
      - 8080
    container_name: api

What I tried already ?

  1. Changed xdebug.ini config to use remote host docker.for.mac.localhost, then host.docker.internal but no success.
  2. Changed start_with_request=trigger instead of yes.
  3. Disabled Mac firewall thinking that maybe port 9001 cannot be used by Docker.

CodePudding user response:

xdebug.discover_client_host=1 is most certainly not correct for use with Docker, you need to set that to 0. discover_client_host uses the HTTP headers to see where the request came from, but as NAT gets involved with Docker Xdebug (and PHP) will see an IP address or host name it can not connect to.

Setting xdebug.client_host=host.docker.internal should be correct with Docker, and this should be the network gateway address, which is what your host is too.

(Even if xdebug.discover_client_host=1, as Xdebug will fall back to the xdebug.client_host setting if it can't connect to the incorrect one it found with HTTP header discovery.)

If you want to see more detailed logs, also add xdebug.log_level=10, which should give more insight.

You don't need "bind port" in docker-compose.yaml for Xdebug, as Xdebug does make the connection to the IDE, and hence the port does not need to be exposed for receiving connections.

  • Related