3

Setup

  • Docker host on Windows using WSL 2 engine
  • Linux (Ubuntu-20.04) WSL 2 Distribution with WSL 2 integration
  • Docker container (webdevops/php-apache-dev) started from Ubuntu
  • Container port mapping 8000:8000

The PHP Web Application in the docker container is now reachable from Windows host via http://localhost:8000

Running PhpStorm on Windows and this Xdebug settings in the php-apache-dev container:

xdebug.remote_connect_back = 0
xdebug.remote_host = host.docker.internal

Xdebug connects to PhpStorm at port 9000, debugging is fine.

xdebug log:

I: Connecting to configured address/port: host.docker.internal:9000.
I: Connected to client. :-)
...

If I run PhpStorm on Ubuntu, Xdebug is not able to reach PhpStorm.

Xdebug Log:

I: Connecting to configured address/port: host.docker.internal:9000.
E: Time-out connecting to client (Waited: 200 ms). :-(

How am I able to debug the php application in PhpStorm starting from my WSL2 Ubuntu?

The reason why is performance. PhpStorm file indexing in Ubuntu is quite faster than from Windows via \\wsl$\Ubuntu-20.04

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Nico W
  • 61
  • 4
  • 1
    If i start a sshd service on Ubuntu, a `ssh host.docker.internal` from the container opens a shell on Ubnutu as expected. But not the xdebug connection still does not work. – Nico W Nov 30 '20 at 14:12
  • 1
    AFAIK `host.docker.internal` is available on Windows/Mac only and NOT on Linux: https://github.com/docker/for-linux/issues/264 (although some new Docker builds may have that already: https://github.com/docker/for-linux/issues/264#issuecomment-714253414). So check what IP the `host.docker.internal` domain gets resolved to (check must be done from **inside the docker container** and not elsewhere). In general, check out https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container – LazyOne Nov 30 '20 at 14:17
  • 2) The connection ...it may still need to pass the Windows Firewall. There should be some rule/command to seamlessly forward connections from WSL2 back to the host OS (where IDE is running). Something like https://github.com/microsoft/WSL/issues/4585#issuecomment-610061194. In any case: check this one as well: https://stackoverflow.com/a/63420260/783119 – LazyOne Nov 30 '20 at 14:21
  • Whatever your solution is: if it works OK for you -- post it as an answer (which you can accept later) -- it may help other users in a similar situation. – LazyOne Nov 30 '20 at 17:06
  • 1
    Disabling windows firewall does not change the behaviour. – Nico W Nov 30 '20 at 17:08

1 Answers1

3

My solution now is a workaround:

  • PHPStorm in Ubuntu listens to port 9099,
  • xdebug in docker container uses xdebug.remote_host = host.docker.internal,
  • Start a ssh tunnel from Windows to Ubuntu with port forwarding 9000.9099: ssh -R 9000:localhost:9099 localhost
Nico W
  • 61
  • 4
  • This solution sounds great, but how did you forward the port exactly? This command returns `ssh: connect to host localhost port 22: Connection refused` regardless if it's launched from WSL2 or from Windows. Is there a config parameter to change in WSL? – Bloops Jan 23 '21 at 07:34
  • 1
    I managed port forwarding with this command to run in Windows Powershell: `netsh interface portproxy add v4tov4 listenport=9003 listenaddress=0.0.0.0 connectport=9099 connectaddress=mywslipaddress` – Bloops Jan 23 '21 at 07:45
  • @Bloops, what's the purpose of this 9003 listen port? Shouldn't it be 9000? – Raisen May 07 '21 at 20:36
  • 1
    @Raisen xdebug3 uses by default 9003. You can forward 9000 as well – Bloops May 09 '21 at 01:08
  • This answer saved my day – glowseed Jan 25 '22 at 13:42