Search code examples
phpdockerdebuggingdocker-composexdebug

xdebug 3, phpstorm configuration under docker installed with wsl 2 ubunto engine with windows 10 as host machine


I have a docker installed on wsl 2 using a program named "micro-env",

enter image description here

The WSL2 fixed IP is :

172.23.183.202

I want to add xdebug 3 to phpstorm, the xdebug is installed in a php 7 docker container.

this is the output of the command :

docker inspect groomrhv2-php -f "{{json .NetworkSettings.Networks }}" --format json | jq . 

Output :

"NetworkSettings": {
      "Bridge": "",
      "SandboxID": "7293963ad4db7c70a3c7e4c24fb7c4e04c26cf4c2c2723a55272aeba90b8fd37",
      "HairpinMode": false,
      "LinkLocalIPv6Address": "",
      "LinkLocalIPv6PrefixLen": 0,
      "Ports": {
        "443/tcp": [
          {
            "HostIp": "0.0.0.0",
            "HostPort": "443"
          },
          {
            "HostIp": "::",
            "HostPort": "443"
          }
        ],
        "80/tcp": [
          {
            "HostIp": "0.0.0.0",
            "HostPort": "8080"
          },
          {
            "HostIp": "::",
            "HostPort": "8080"
          }
        ],
        "9000/tcp": null
      },
      "SandboxKey": "/var/run/docker/netns/7293963ad4db",
      "SecondaryIPAddresses": null,
      "SecondaryIPv6Addresses": null,
      "EndpointID": "",
      "Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "IPAddress": "",
      "IPPrefixLen": 0,
      "IPv6Gateway": "",
      "MacAddress": "",
      "Networks": {
        "groomrhv2_dockerized_default": {
          "IPAMConfig": null,
          "Links": null,
          "Aliases": [
            "groomrhv2-php",
            "groomrhv2-php",
            "c1aa5080ef3e"
          ],
          "NetworkID": "3fb9d9c63780fe121610305d6d164d70508c1ebb3c7de9267ddbba3feff39b58",
          "EndpointID": "e717eecc4374e4f025bf83b98945d87bd8e973ebd7a53d11538fa10bb460f064",
          "Gateway": "172.22.0.1",
          "IPAddress": "172.22.0.5",
          "IPPrefixLen": 16,
          "IPv6Gateway": "",
          "GlobalIPv6Address": "",
          "GlobalIPv6PrefixLen": 0,
          "MacAddress": "02:42:ac:16:00:05",
          "DriverOpts": null
        }
      }
    }
  }

and then inside the container :

docker exec -ti groomrhv2-php bash

The output of php --ini inside container :

Cannot load Xdebug - it was already loaded
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File:         (none)
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed:      /usr/local/etc/php/conf.d/docker-php-ext-config.ini,
/usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
/usr/local/etc/php/conf.d/docker-php-ext-intl.ini,
/usr/local/etc/php/conf.d/docker-php-ext-opcache.ini,
/usr/local/etc/php/conf.d/docker-php-ext-pdo_pgsql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini,
/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,
/usr/local/etc/php/conf.d/error_reporting.ini,
/usr/local/etc/php/conf.d/opcache.ini,
/usr/local/etc/php/conf.d/xdebug.ini

the xdebug extension is in : /usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so

and the php.ini is in : /usr/local/etc/php

In phpstorm i added docker :

enter image description here

and then i added the php cli interpreter like this :

enter image description here

this is my xdebug.ini config :

[xdebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.mode=debug,develop,profile
xdebug.discover_client_host=false
xdebug.client_host=172.23.183.202
xdebug.client_port=9000
xdebug.start_with_request=yes
xdebug.idekey=GroomRHV2
XDEBUG_SESSION=GroomRHV2
xdebug.cli_color=1
xdebug.log_level=3
xdebug.remote_autostart = 1
xdebug.remote_enable = 1

and then i tried to validate debugger conf on web server :

enter image description here

and this is what i finally get :

enter image description here

I'm not currenly enable to use xdebug 3 with phpstorm, i will be grateful if someone could help me ! Thank you


Solution

  • This works for me. So If you have phpstorm installed on Windows and would like to debug php projects using xdebug that is installed inside WSL (Ubuntu or any other distro). Then here's how:

    1. On Ubuntu terminal, get Windows IP (your host system):
    ip route show | grep -i default | awk '{ print $3}'
    
    1. On Ubuntu terminal, get xdebug.ini file path using:
    php --ini | grep xdebug | sed 's/,$//'
    
    1. Open the xdebug file at the path you get from step 2 like this:
    # replace the path with the one you get from step 2
    sudo nano /etc/php/8.3/cli/conf.d/20-xdebug.ini
    
    1. Modify xdebug.ini by adding Windows IP as the value for xdebug.client_host:
    xdebug.mode=debug
    xdebug.client_host=172.25.0.1 # replace this with Windows IP from step 1
    

    then save it (CTRL + S) and exit (CTRL + X).

    1. Do other needed steps on Phpstorm: (1) listening for connection (2) installing Chrome extension (3) mapping files.

    2. Start a debug session with Chrome extension (on DEBUG mode), listening for Connection (ON) inside Phpstorm, and setting a break point or break on first line. and it should work!