wp-local-docker-v2: Xdebug Not Working with Windows 10, WSL and VS Code

Describe the bug I just started using WP Local Docker, so thank you so much for developing such a great resource. Since I’m not super familiar with Linux and Docker, it’s taking me some time to get up to speed. The good news is that I’ve got most things configured at this point. The only item I can’t figure out is Xdebug.

When I try to run debugging using VS Code, breakpoints don’t work and I get this message in debug.log on every page load:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: 10.0.128.3:9003 (from HTTP_X_FORWARDED_FOR HTTP header), host.docker.internal:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

I’ve used Xdebug a lot in the past, so I’m pretty confident I know how to use it generally, but I can’t seem to get it working with Windows 10, WSL and Docker. Can you give me any advice?

Thanks a ton.

Environment information

  • Device: Acer Laptop
  • OS: Windows 10 with WSL
  • Docker Desktop version: 4.5.1
  • Browser and version: Chrome v99.0.4844.51
  • WordPress version: 5.9.2

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 35 (5 by maintainers)

Most upvoted comments

I was finally able to get Xdebug working with WSL 2, Docker and WP Local Docker. I followed very closely to the steps outlined at https://github.com/10up/wp-local-docker-v2/issues/294#issuecomment-1073308040.

Here are the steps I followed in case this will help someone else:

  1. Start Docker Desktop.
  2. Within the WSL 2 terminal enter this command to get the needed IP address: ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
  3. Open the file at /config/php-fpm/docker-php-ext-xdebug.ini within the WP Local Docker website you’re working on. Set xdebug.client_host to be whatever IP address was returned in step 2. For example, the final line might look like xdebug.client_host = 192.168.119.150. Until we can find a better solution, you’ll need to repeat steps 2 and 3 every time WSL 2 restarts.
  4. Run 10updocker stop all in the WSL 2 terminal.
  5. Run 10updocker start env-name in the WSL 2 terminal. Make sure to replace env-name with the name of your environment.
  6. If you get the error Xdebug: [Step Debug] Time-out connecting to debugging client... take a look at https://xdebug.org/docs/all_settings#start_with_request. It’s likely Xdebug is running on every page load, even if VS Code isn’t debugging.

For what it’s worth @LoveDuckie, I didn’t need to change the Remote.WSL2: Connection Method setting in VS Code. It worked with the default wslExeProxy option for me.

Thanks again for the help @LoveDuckie and @nicholasio.

This might be an issue with WSL 2 not forwarding ports properly. I can’t provide an in-depth explanation of the issue but here’s how I managed to get XDebug working under WSL 2.

1- Get your WSL 2 IP Adress with ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' 2 - Add a manual host entry to your Windows 10 hosts file that points to that IP (e.g: wsl2.test) 3 - Edit config/php-fpm/docker-php-ext-xdebug.ini and add/replace the following line:

xdebug.remote_host = wsl2.test ; replace with your host name or IP address

4 - Perform a global restart 10updocker stop all && 10updocker start env-name

Keep in mind that WSL 2 Ip address will change every time you restart your machine or just WSL itself. I know it’s possible to have windows automatically update the hosts file with the correct IP address but I haven’t managed to get that running.

FWIW: I think we would not have this problem if we were running the docker daemon inside WSL 2 directly instead of Docker Desktop.

@LoveDuckie, unfortunately not. I dropped a lot of hours trying to make it happen with no luck. I’m sure there’s a way. I just wasn’t able to devote anymore time to it.

I’m hoping to work on it again in the future, but I’m not sure when that will be.

Good luck and let me know if you figure it out.

@jg314 Thanks for the super fast response!

I’ll definitely let you know if I find a fix. I think the problem is something to do with port-forwarding between the Visual Studio Code process running in windows, and the extension listening on port 9003 in WSL2. Not 100% sure what the problem is but I think I’m sort of close.