Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run php-xdebug in docker(WSL2) #5277

Closed
slps970093 opened this issue May 28, 2020 · 10 comments
Closed

Can't run php-xdebug in docker(WSL2) #5277

slps970093 opened this issue May 28, 2020 · 10 comments

Comments

@slps970093
Copy link

I tried to set up a PHP development environment in Docker in WSL 2 mode, and it does not work with Xdebug

@mauroartizzu
Copy link

It's happening to me too. It worked, maybe one of the latest updates broke something

[43]
[43] Log opened at 2020-06-05 15:42:31
[43] I: Connecting to configured address/port: 172.18.245.167:9001.
[43] E: Time-out connecting to client (Waited: 200 ms). :-(
[43] Log closed at 2020-06-05 15:42:32

I have a docker-compose file which declares the following ENV VAR

environment: 
  - XDEBUG_CONFIG=remote_host=${IP} remote_log=/data/log/xdebug.log

inside the WSL host I have this line in my .zshrc

export IP=$(hostname -I)

So everytime I restart the WSL or reboot my computer docker-compose updates his ip correctly.

This was working good until last one or two weeks I think. Now suddenly stopped working both in PHPStorm and VSCode

My VSCode config is like this

{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "Listen for XDebug",
        "type": "php",
        "request": "launch",
        "port": 9001,
        "pathMappings":{
            "/data": "${workspaceRoot}"
        }
    }]
}```

@hectorviov
Copy link

Same situation it works correctly if I use:

[xdebug]
xdebug.remote_autostart = 1
xdebug.remote_enable = 1
xdebug.remote_port = 9009
xdebug.remote_host = 172.27.123.116
xdebug.idekey = VSCODE

remote_host's IP is the WSL2 host IP
But if I use:

xdebug.remote_host = localhost

Or any other thing that is not the literal IP it doesn't work, i get the same E: Time-out connecting to client (Waited: 200 ms). :-( error

@mauroartizzu
Copy link

@hectorviov that is correct, you need to use the host ip, your remote is not localhost, the problem is wsl2 ip changes, in my issue the problem is it's not connecting even with that ip, I had to revert my docker for windows

@jakedowns
Copy link

jakedowns commented Jul 6, 2020

i'm having the same issue. somehow xdebug never seems to pick up on the request from the browser.
I'm trying to follow this guide currently to see if it's any help: https://www.silverf0x00.com/setting-up-xdebug-for-phpstorm-on-windows-wsl2/ so far i'm not having much luck.

I've tried setting xdebug.remote_host= manually to the IP of my wsl2 instance, and i've tried host.docker.internal which doesn't seem to work either. dunno if it's port 9000 that isn't making it all the way through or what. will keep digging...

quote-linking related issue: docker/for-win#6993

See my other comment for updates: docker/for-win#6993 (comment)

@mauroartizzu
Copy link

mauroartizzu commented Jul 7, 2020

@jakedowns I solved by fetching the ip via hostname -I and updating my config with sed

something like

IP=$(hostname -I)
sed -i -e "s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/$IP/g" docker-compose-windows.yml

and this is my docker-compose snippet

php:
    environment:
      - XDEBUG_CONFIG=remote_host=123.123.123.123

@therealkenc
Copy link
Collaborator

There are no repro steps in this issue. There's a good chance it can be duped if someone can post the output of (from inside WSL2):

$ cd /mnt/c
$ netstat -nlt
$ netstat.exe -an -p TCP | grep 9000

@stevenobird
Copy link

This is my output when executing the commands you've provided. Being in /mnt/c should make no diffence because Windows' %PATH% will be translated and merged into the Linux $PATH - at least it should by default.

/mnt/c
➜ netstat -nlt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:6443          0.0.0.0:*               LISTEN
tcp6       0      0 :::8080                 :::*                    LISTEN
tcp6       0      0 :::8081                 :::*                    LISTEN
tcp6       0      0 :::8085                 :::*                    LISTEN
tcp6       0      0 :::8025                 :::*                    LISTEN
tcp6       0      0 :::45255                :::*                    LISTEN
tcp6       0      0 :::3306                 :::*                    LISTEN

/mnt/c
➜ netstat.exe -an -p TCP | grep -a 9000
  TCP    0.0.0.0:9000           0.0.0.0:0              ABH�REN

In my case, the grep for the netstat.exe command needed the -a switch so that the (binary) output from netstat.exe is processed as text. That weird state is because my Windows locale is set to German. Is just means "LISTEN".

Its quite interesting that the Linux netstat lists all ports that are exposed by docker-compose containers as tcp6 bindings. Port 9000 is in LISTEN state in Windows, because I have enabled PHPStorm to "Listening for PHP Debug Connections".

I am using Docker for Windows as daemon/service here, if that might be important:

  • Version 2.3.0.4 (46911)
  • Channel stable
  • Engine 19.03.12
  • Compose 1.26.2

I am running everything on a Windows 10 Pro:

  • Version 2004
  • OS Build 19041.508

I also do not get Xdebug working when running it in a container inside a WSL2 instance (Ubuntu 20.04).
By "not working" I mean that Xdebug indeed detects the requests from my browsers, but cannot connect back to the client (which is PHPStorm listening for connectionc. The setting I've been using when trying to get it working was xdebug.remote_connect_back=1.

It only works when setting the xdebug.remote_host to the exact host ip from the WSL2 host (and dropping xdebug.remote_connect_back or setting it to 0).

So my current workaround is almost the same as already mentioned: I export an $WSLIP environment variable in my .bashrc or .zshrc file and reference to it in my docker-compose.yml file which delegates that to build args for a Dockerfile that is building the actual image.

docker-compose.yml:

services:
  app:
    build:
      context: ./docker/php-fpm
      args:
        xdebug_remote_host: ${WSLIP}

Dockerfile inside of ./docker/php-fpm:

FROM php:7.4-fpm

# 0.0.0.0 will be the default if it is not set by docker-compose
ARG xdebug_remote_host=0.0.0.0
COPY dev/install_xdebug.sh .
RUN chmod +x ./install_xdebug.sh && ./install_xdebug.sh

install_xdebug.sh in ./docker/php-fpm/dev:

echo "Installing XDebug..."
echo "|-> Using ${xdebug_remote_host} as xdebug.remote_host configuration."
pecl install xdebug-2.9.6 \
    && docker-php-ext-enable xdebug \
    && echo 'zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so"' > $PHP_INI_DIR/php.ini \
    && echo 'xdebug.remote_port=9000' >> $PHP_INI_DIR/php.ini \
    && echo 'xdebug.remote_enable=1' >> $PHP_INI_DIR/php.ini \
    && echo "xdebug.remote_host=${xdebug_remote_host}" >> $PHP_INI_DIR/php.ini

@john-everden
Copy link

I'm at a loss I can't seem to get it to work at all in PHP. I do have the site running with a reverse proxy in traefik then php running as FPM. I've been attempting to validate settings in PHPStorm as network mounted directory and as a remote server. Running hostname -I results in:

172.31.172.163 172.20.0.1 172.29.0.1 172.21.0.1 172.18.0.1 172.30.0.1 172.26.0.1 172.22.0.1 172.28.0.1 172.17.0.1 172.19.0.1 172.24.0.1 172.27.0.1 172.25.0.1 172.23.0.1 192.168.0.1

I believe 172.23.0.1 is the correct hostname (the proxy passes this as the original IP), I've tried adding this, adding all the ips comma separated, and adding all the ips with a space to no avail.

PHP storm setting is set to 9090 for xdebug port. In PHP storm I simply get
Validation script was created but it cannot be executed, check possible reasons:
Configured 'Url to validation script' is incorrect
Validated Web Server is not running.

Target directory for validation script is not public.

My PHP ini looks like this:
xdebug.remote_enable=1
xdebug.remote_host=172.23.0.1
xdebug.remote_port=9090
xdebug.remote_log=/xdebug_log

I have confirmed used phpinfo() the extension is installed and enabled.

I have port 9090 bound to port 9090 in the docker container. This appears to show up with netstat -nlt:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp6 0 0 :::443 :::* LISTEN
tcp6 0 0 :::3005 :::* LISTEN
tcp6 0 0 :::1025 :::* LISTEN
tcp6 0 0 :::9090 :::* LISTEN
tcp6 0 0 :::1026 :::* LISTEN
tcp6 0 0 :::3399 :::* LISTEN
tcp6 0 0 :::3367 :::* LISTEN
tcp6 0 0 :::9000 :::* LISTEN
tcp6 0 0 :::9033 :::* LISTEN
tcp6 0 0 :::3344 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN
tcp6 0 0 :::8085 :::* LISTEN
tcp6 0 0 :::9301 :::* LISTEN
tcp6 0 0 :::9207 :::* LISTEN
tcp6 0 0 :::8025 :::* LISTEN

I've attempted to shutdown both the windows firewall and UFW as well. I'm open to any suggestions having debugging would significantly increase my team's performance and most of us are using WSL2!

@therealkenc
Copy link
Collaborator

There's a good chance it can be duped if someone can post the output of [netstat.exe]

/dupe #4851

@ghost
Copy link

ghost commented Sep 24, 2020

Hi! We've identified this issue as a duplicate of another one that already exists in this repository. This specific instance is being closed in favor of tracking the concern over on the referenced thread.

Thanks for your report!

@ghost ghost closed this as completed Sep 24, 2020
@ghost ghost added the duplicate label Sep 24, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants