Could not connect to debugging client - Drupal, Docker and Xdebug issue

Recently I have upgraded my Docker4Drupal environment while xdebug in PHPStorm stopped working for me. Investigating PHP container logs, revealed the following message:

Xdebug: [Step Debug] Could not connect to debugging client. 
Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

It turns out that the latest version of xdebug is using port 9003 port by default instead of 9000. Since my PHPStorm was still configured to port 9000, I had to change it. Start the debugger again and ... nothing, still not working :(

Xdebug not stopping at my breakpoint

The solution to this problem is setting xdebug.discover_client_host. As the log message from above describes Docker is trying to send debug information to the localhost which does not make sense. In my case, this setting was disabled by default.

This setting can be overridden in the docker-compose.yml where PHP service environment variables are set. The important change is the XDEBUG_CONFIG variable which enables the xdebug.discover_client_host. Updated docker-compose.yml should look like this:

services:
  php:
    environment:
      PHP_XDEBUG: 1
      PHP_XDEBUG_MODE: debug
      PHP_IDE_CONFIG: serverName=d8-docker
      PHP_XDEBUG_IDEKEY: "PHPSTORM"
      PHP_XDEBUG_START_WITH_REQUEST: "yes"
      PHP_XDEBUG_REMOTE_HOST: 172.17.0.1 # Linux
      # Solution for the problem described above
      # For more settings see: https://xdebug.org/docs/all_settings
      XDEBUG_CONFIG: "discover_client_host=1"

As a bonus, you can also set the xdebug.client_host setting which translates to your host machine IP address as a fallback. This will be used when it fails to resolve the client host IP.

References

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.