繁体   English   中英

Xdebug 尝试连接到错误的地址(Xdebug、PHP、Docker、VSCode、WSL2)

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

我在 docker 容器中有开发环境。 我正在尝试运行 Xdebug 并从主机上运行的 VSCode 连接到它(Windows 10)。

我尝试在 docker-compose 中使用

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

但是我invalid IP address in add-host: "host-gateway的错误

挖掘后发现可能与docker 19版本有关,于是尝试升级。

现在我有奇怪的情况:

从 Windows cmd:

# docker -v
Docker version 20.10.8, build 3967b7d

从 WSL 内部:

$ docker -v
Docker version 19.03.8, build afacb8b7f0

无论如何,它并没有解决问题。

Xdebug 安装在容器中:

/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

这是 Xdebug 日志:

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. :-(

端口 9001 在 dockerfile 和 docker-compose 文件中公开。

Xdebug 配置:

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

host.docker.internal甚至无法识别:

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

那么Xdebug日志文件中的172.20.112.1是从哪里来的呢?

集装箱路线:

# 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

容器hosts文件:

# 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

Windows 路由表:

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

如果您需要更多信息,请告诉我

我在提出一个包含设置以连接到在容器内运行的 xdebug 时遇到了很多问题,但目前以下设置对我有用:

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

我在构建期间将 xdebug 添加到 docker 中。 有一个选项可以在运行时(向上)使用卷来执行此操作,这允许在不需要构建的情况下动态更改它,但它不太可靠。

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

不要在 dockerfile 中公开端口。

以下是xdebug.remote_host值的选项。 我注意到时不时地,在我的系统发生一些变化后,另一个 ip 可以工作。 一般来说,ip 应该可以从容器内访问(所以安装 ping、 apt-get install iputils-ping并检查是否收到响应)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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