简体   繁体   中英

Xdebug tries to connect to the wrong address (Xdebug, PHP, Docker, VSCode, WSL2)

I have development environment inside docker containers. I am trying to run Xdebug and connect to it from VSCode running on the host (Windows 10).

I tried to use in docker-compose

        extra_hosts:
        - "host.docker.internal:host-gateway"

But I got an error that invalid IP address in add-host: "host-gateway

After digging I found that it might be related to docker version 19. So I tried to upgrade.

Now I have weird situation:

from Windows cmd:

# docker -v
Docker version 20.10.8, build 3967b7d

from inside WSL:

$ docker -v
Docker version 19.03.8, build afacb8b7f0

Anyway, it didn't solve the problem.

Xdebug is installed in the container:

/var/www/html# php -v
PHP 5.6.40 (cli) (built: Jan 23 2019 00:10:05) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

This is the Xdebug log:

Log opened at 2021-11-04 20:58:06
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.20.112.1:9001.
E: Time-out connecting to client. :-(

Port 9001 is exposed in the dockerfile and in the docker-compose file.

Xdebug config:

xdebug.extended_info=on
xdebug.idekey=xdebug
xdebug.profiler_enable=off
xdebug.profiler_enable_trigger=on
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=host.docker.internal
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_port=9001

but host.docker.internal isn't even recognized:

# ping host.docker.internal
ping: host.docker.internal: Name or service not known

so where does 172.20.112.1 in the Xdebug log file coming from?

the container route:

# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.18.0.1      0.0.0.0         UG    0      0        0 eth0
172.18.0.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0

the container hosts file:

# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3      myappname

The Windows routing table:

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0       10.0.0.138        10.0.0.15     35
         10.0.0.0    255.255.255.0         On-link         10.0.0.15    291
        10.0.0.15  255.255.255.255         On-link         10.0.0.15    291
       10.0.0.255  255.255.255.255         On-link         10.0.0.15    291
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    331
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    331
       172.17.0.0      255.255.0.0    172.31.232.91     172.20.112.1   5001
       172.18.0.0      255.255.0.0    172.31.232.91     172.20.112.1   5001
     172.20.112.0    255.255.240.0         On-link      172.20.112.1   5256
     172.20.112.1  255.255.255.255         On-link      172.20.112.1   5256
   172.20.127.255  255.255.255.255         On-link      172.20.112.1   5256
     172.28.128.0    255.255.255.0         On-link      172.28.128.1    281
     172.28.128.1  255.255.255.255         On-link      172.28.128.1    281
   172.28.128.255  255.255.255.255         On-link      172.28.128.1    281
     192.168.48.0    255.255.240.0         On-link      192.168.48.1   5256
     192.168.48.1  255.255.255.255         On-link      192.168.48.1   5256
   192.168.63.255  255.255.255.255         On-link      192.168.48.1   5256
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    331
        224.0.0.0        240.0.0.0         On-link      172.28.128.1    281
        224.0.0.0        240.0.0.0         On-link         10.0.0.15    291
        224.0.0.0        240.0.0.0         On-link      192.168.48.1   5256
        224.0.0.0        240.0.0.0         On-link      172.20.112.1   5256
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    331
  255.255.255.255  255.255.255.255         On-link      172.28.128.1    281
  255.255.255.255  255.255.255.255         On-link         10.0.0.15    291
  255.255.255.255  255.255.255.255         On-link      192.168.48.1   5256
  255.255.255.255  255.255.255.255         On-link      172.20.112.1   5256
===========================================================================
Persistent Routes:
  Network Address          Netmask  Gateway Address  Metric
       172.17.0.0      255.255.0.0    172.31.232.91       1
       172.18.0.0      255.255.0.0    172.31.232.91       1

If you need more info, please let me know

I have many issues with coming up with a consist setup to connect to xdebug running inside a container, but currently the following setup is what works for me:

xdebug.ini:

xdebug.extended_info=on
xdebug.idekey=VSCODE
xdebug.profiler_enable=off
xdebug.profiler_enable_trigger=on
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=172.22.241.214
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_port=9001

I add xdebug into the docker during build. There is an option to do it using volumes on runtime (up), which allows changing it on the fly without requiring build, but it is less reliable.

Dockerfile:

RUN pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug
RUN touch /var/log/xdebug.log \
    && chmod 777 /var/log/xdebug.log
ADD config/xdebug.ini /usr/local/etc/php/conf.d/20-xdebug.ini

Do not expose the port in the dockerfile.

Following are options for xdebug.remote_host value. I have noticed that every now and then, after some change in my system, another ip works. In general, ip should be reachable from within the container (so install ping, apt-get install iputils-ping , and check if you get a response).

- route print -4  => persistent routes => gateway address
- ipconfig address v4
- netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'

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