简体   繁体   中英

xDebug not working using docker, vscode and WSL 2

I am not sure what the issue is, it just doesn't work.

The routing seems to work, I have a server name in my nginx conf file. eg test.com. that works.

My project is in the root of ubuntu and not in the mount folders.

I am not sure what else to try.

xdebug.ini

[XDebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so

xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9002
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=soapboxtest.com
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log

launch.json

{
  "name": "Listen for XDebug",
  "type": "php",
  "request": "launch",
  "port": 9002,
  "log": true,
  "externalConsole": false,
  "pathMappings": {
    "/var/www": "${workspaceRoot}"
  },
  "ignore": [
    "**/vendor/**/*.php"
    ]
  },

Request cookies

"XDEBUG_SESSION" => "VSCODE"

Dockerfile

FROM php:fpm-alpine3.11
...
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
...

This happened to me also and found that XDEBUG is not looking for the Docker Daemon Host. Got it fixed by adding:

"hostname": "0.0.0.0"

As part of the general options of the launch.json on VS Code.

I have spent many days "ripping my hair".. I use docker.

  1. add rule into firewall to allow traffic (taken/quoted from: https://github.com/microsoft/WSL/issues/4171#issuecomment-559961027
    Go there for screenshots if this isn't clear enough)
  1. In windows Start menu, type Firewall and choose Advanced settings , then Inbound Rules
  2. select custom rule.
  3. Click Next until you get to Protocol and Ports in the left menu
  4. Select ICMPv4 as protocol type.
  5. Select Next until you end up in the Name section, enter any name and click Finish.

If you now try pinging your host ip from wsl2, it should work as expected.

For every other connection between your wsl2 and your host, you have to allow the inbound rules for private and public networks or if they don't exists manually create the rules for the corresponding UDP/TCP port, but be aware that this might impact your security, if you use your computer in public networks.

  1. .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9900,
            "pathMappings": {
                "/linux/path/in/docker":"${workspaceFolder}",
            }
        },
    ]  
}
  1. Dockerfile
 RUN echo "zend_extension=/usr/lib/php/20190902/xdebug.so\n \ xdebug.remote_enable=on\n \ xdebug.remote_autostart=on \n \ xdebug.remote_connect_back=on\n \ # xdebug.remote_host=$HOST_IP\n \ # xdebug.remote_log_level=9\n \ # xdebug.remote_log=/var/www/mywebdir.com/xdebug.log \n \ xdebug.remote_port=9900" > /etc/php/7.4/mods-available/xdebug.ini;

EDIT: After a while I started having problems again. Try these:

  • uncomment the logging lines above in dockerfile

  • try hard coding your windows ip address like 192.168.0.x

    (uncomment xdebug.remote_host=... and comment out xdebug.remote_autostart=on)

    My xdebug appeared to be connected but closed connection straight after because it had to be my windows ip. My understanding is that its the VSCode that responds to xdebug requests which runs in windows so the debugging packets need to get through out of WSL2.

  • try logging level 10. If you see that xdebug can't find a match for the breakpoints, check your webroot mapping in docker and pathMappings in vscode/launch.json

  • Check that you do have a breakpoint set (stupid but yeah)

After had a colleague helping me to configure Xdebug to listen out of WSL Remote environment from VSCode, I´ve looked up for an option that is opened when you click on the WSL Remote icon on the lower left side of the IDE:

Remote-WSL: Show Log

It opens a WSL terminal , which shows (between several infos) the IP Address that WSL is using . Adding this WSL IP to the xdebug.remote_host value, the only thing left to get Xdebug running correctly from your Docker container, inside your WSL2 environment (from WSL Remote VSCode extension), is adjusting your application path inside the launch.json file.

For that, clicking with the right-button mouse at the launch.json file tab, search and click on the "Copy path" option, and then paste it inside the pathMappings setting, like this sample (don´t forget to adjust the path removing the info relative to launch.json itself, it's for your application path:):

"pathMappings": {
      "/application": "paste here the path you've copied"
 }

Try it, for me worked like a charm. And if it work for you too, vote for my answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM