XDebug 无法从 Docker 容器连接到客户端

4

我有几个 Docker 容器:php、nginx、mariadb、redis、adminer,并尝试使用 PhpStorm 和 Xdebug 调试一些 PHP 代码。

主机(联想 T490 笔记本电脑):

$ hostname
T490


$ docker -v
Docker version 20.10.5, build 55c4c88


$ docker-compose -v
docker-compose version 1.24.1, build 4667896b


$ cat /etc/lsb-release 
DISTRIB_ID=neon
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="KDE neon User Edition 5.21"


$ php -v
PHP 7.4.16 (cli) (built: Mar  5 2021 07:54:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies


$ lsof -i :9003 | grep LISTEN
java    14868 kane   48u  IPv6 401093      0t0  TCP *:9003 (LISTEN)


$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       T490

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

172.17.0.1 foo.pl adm.foo.pl api.foo.pl

PhpStorm 2020.3.3 配置:

这里输入图片描述

这里输入图片描述

Docker-compose.yml 文件

version: "3"
services:
  php-7.4:
    extra_hosts:
      - "host.docker.internal:host-gateway"
    build: ./docker-images/php-7.4
    image: ap/php:7.4
    container_name: foo-php-7.4
    environment:
      - GITHUB_API_TOKEN=${GITHUB_API_TOKEN}
      - XDEBUG_IDEKEY=${XDEBUG_IDEKEY}
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ~/.gitconfig:/root/.gitconfig
      - ${WORKSPACE_DIR}:/var/www
      - ~/.ssh:/root/ssh:ro
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

使用DockerFile安装XDebug,可以通过以下步骤实现:

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

.env

GITHUB_API_TOKEN=
WORKSPACE_DIR=/home/kane/workspace

XDEBUG_IDEKEY=PHPSTORM

COMPOSE_PROJECT_NAME=yii2fpm
COMPOSE_FILE=docker-compose.yml
X_LEGACY_GD_LIB=1
PHP_CGI_PASS=php-7.4:9000
NGINX_PORT=80
NGINX_SSL_PORT=443
DB_PORT=3306
ADMINER_PORT=8182

xdebug.ini

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=172.17.0.1
xdebug.start_with_request = yes

在运行docker-compose up后,在容器中:
# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         T490            0.0.0.0         UG    0      0        0 eth0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0


# ping T490
PING T490 (127.0.1.1) 56(84) bytes of data.
64 bytes from T490 (127.0.1.1): icmp_seq=1 ttl=64 time=0.057 ms


# ping 172.17.0.1
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.145 ms


# telnet 172.17.0.1 9003
Trying 172.17.0.1...


# telnet T490 9003
Trying 127.0.1.1...
telnet: Unable to connect to remote host: Connection refused


# php -v
PHP 7.4.16 (cli) (built: Mar 13 2021 02:52:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
    with Xdebug v3.0.3, Copyright (c) 2002-2021, by Derick Rethans

当我访问 http://adm.foo.pl/(使用cookie XDEBUG_SESSION:"PHPSTORM")时,docker日志显示:

NOTICE: PHP message: Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.17.0.1:9003 (through xdebug.client_host/xdebug.client_port) :-(

将xdebug.ini更改为以下内容后:

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.discover_client_host = yes
xdebug.start_with_request = yes

原文:

提示信息: PHP信息: Xdebug: [Step Debug] 无法连接调试客户端。尝试:172.18.0.1:9003(从REMOTE_ADDR HTTP标头),localhost:9003(通过xdebug.client_host / xdebug.client_port回退): -(

添加extra_host后,提示信息如下:

提示信息: PHP信息: Xdebug: [Step Debug] 连接调试客户端超时,等待时间为200毫秒。尝试:host.docker.internal:9003(通过xdebug.client_host/xdebug.client_port): -(

请帮忙解决问题 :-)

5个回答

5

很遗憾... 通常我使用Centos7,所以我通过firewall-cmd而不是ufw(在Ubuntu / Debian上)查找防火墙。

$ sudo ufw allow 9003

现在一切都开始按照预期运作。


只是出于好奇,它也能与172.17.0.1一起工作吗? - Eugene Morozov
嗯,在容器中 telnet 172.17.0.1 9003 是可以工作的。XDebug 日志显示 [Step Debug] INFO: Client host discovered through HTTP header, connecting to 172.18.0.1:9003. [34] [Step Debug] INFO: Connected to debugging client: 172.18.0.1:9003 (from REMOTE_ADDR HTTP header). :-) - TomaszKane
1
哦,那是 discover_client_host 的问题,在 Docker 中无法可靠工作。然后,除了 ufw 部分之外,您的初始设置也是正确的。 - Eugene Morozov
我遇到一个类似的问题,但是我在使用 Windows 系统。你有什么解决方法吗? - techcase

4

在 Linux 上使用 Docker 时,您可以选择:

  1. 使用物理网络接口(如 ens*、eth0 或类似的接口)IP 连接到主机,或者
  2. 使用这个 hack,以便能够使用 host.docker.internalhttps://github.com/docker/for-linux/issues/264#issuecomment-759737542

在容器中安装 telnet 来检查端口的可用性总是一个好主意,事实上 172.17.0.1 响应 ping 并不一定表示它是主机。


你好,我在docker-compose中为我的php服务添加了extra_hosts,停止/启动容器并安装了telnet。为什么无法连接? - TomaszKane

2
在我的情况下,Ubuntu 的防火墙(ufw)并不是问题的原因:而是 rootless Docker 阻止了容器内的 XDebug 连接到主机上的 IDE。XDebug 需要一个名为 br-xxxx 的桥接网络接口,并且属于与 PHP 容器相同的网络范围。在非特权模式下无法创建此桥接接口(只能创建 Docker 的 docker0)。我的另一个目标是继续在 compose.yml 中使用 host.docker.internal(而不是以太网或 Wi-Fi 的 192.168.x.x)。
对我有效的解决方案是:
  1. 将以下内容添加到 compose.yml 中的 PHP 服务中
extra_hosts:
      host.docker.internal: host-gateway

添加到docker-php-ext-xdebug.ini文件中
xdebug.client_port=9003
xdebug.client_host=host.docker.internal

从这里获取:
  1. 使用sudo运行docker,例如sudo docker compose up -d

取自这里:


1
在花了几个小时解决这个问题后,我决定与主机的IP地址192.168.x.x进行对话,而不是172.17.0.1。这样做有效果。
在Windows 10上,可以通过以下步骤确定x.x的确切值:
  • 开始 -> 命令提示符(以管理员身份运行)
  • 运行ipconfig | find "IPv4"命令
  • 使用192.168...的值。
这是我的xdebug配置现在的样子:
xdebug.client_host=192.168.1.110
xdebug.client_port="9003"
xdebug.start_upon_error=yes
xdebug.idekey = "PHPStorm"
xdebug.mode=develop,coverage,debug,profile
xdebug.discover_client_host=1

0
我在下面的docker-compose.yml配置中有两个值,删除第二个后又开始正常工作了。
来自
extra_hosts:
  - "host.docker.internal:host-gateway"
  - "host.docker.internal:127.0.0.1"

To:

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

我不知道为什么我的配置是这样的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接