Xdebug 3 无法从 Docker 连接 [20.10.12 |无根模式]到 PhpStorm 调试客户端。乌班图

问题描述 投票:0回答:2

所以,我的配置是:

  1. Dockerfile:
FROM php:8.1-apache as php_base
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN a2enmod rewrite && install-php-extensions pgsql
RUN echo 'ServerName localhost' >> /etc/apache2/apache2.conf && service apache2 restart
WORKDIR /var/www/lessor-calculator

FROM php_base as php_dev
RUN pecl install xdebug && docker-php-ext-enable xdebug
  1. docker-compose.yml:
version: "3.4"

services:
  app:
    build:
      context: docker/server
      target: php_dev
    networks:
      my-network:
    environment:
      PHP_IDE_CONFIG: "serverName=MyApp"
    ports:
      - "${DOCKER_SERVER_PORT:-9180}:80"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./:/var/www/lessor-calculator
      - ./docker/server/apache/sites-enabled:/etc/apache2/sites-enabled
      - ./docker/server/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./docker/server/php/conf.d/xdebug.log:/var/log/xdebug.log
      - ./docker/server/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
  1. xdebug_info()
    输出:

  1. PhpStorm 设置:

  1. X调试日志:
[17] Log opened at 2022-01-04 14:47:45.645181
[17] [Step Debug] INFO: Connecting to configured address/port: host.docker.internal:9003.
[17] [Step Debug] WARN: Creating socket for 'host.docker.internal:9003', poll success, but error: Operation now in progress (29).
[17] [Step Debug] ERR: Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port) :-(
[17] Log closed at 2022-01-04 14:47:45.649491

我还尝试使用另一个端口,

curl host.docker.internal:9000
,答案为“连接被拒绝”(但只需 ping
host.docker.internal
就可以正常工作)。大家都说,不需要映射9000端口。 请任何建议,可能是什么原因。谢谢你。

更新:Xdebug 配置:

zend_extension=xdebug

[xdebug]
xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.log="/var/log/xdebug.log"

更新:9003端口的新配置,启用xdebug.log

更新2:我正在使用无根docker。我已经检查过这一切都在 rootfull 模式下工作。

php docker apache phpstorm xdebug
2个回答
0
投票

它对我的案例有用:

PHP 8.2.8 (cli) (建于: Jul 8 2023 07:10:21) (NTS) 版权所有 (c) PHP Group Zend Engine v4.2.8,版权所有 (c) Zend Technologies 使用 Zend OPcache v8.2.8,版权所有 (c),作者:Zend Technologies 使用 Xdebug v3.2.1,版权所有 (c) 2002-2023,作者:Derick Rethans

在 docker-compose.yaml 文件中,有 2 行需要重新配置,如下所示

XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-debug}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=172.26.174.72}'

正如@LazyOne 所发现的那样, 如果我们使用 VSCode 远程连接到 WSL(源代码放入 WSL 中),则 client_host 将是 wsl 的 IP。如果 VSCode 直接打开工作区那么它必须是机器的 IP(Host Machine)。

补充:

要查找 IP,请在目标环境终端上运行 ifconfig、ipconfig 或类似的命令。

在 WSL 中运行

Sail php -m 
检查 Docker 容器上是否安装了 xdebug。

如果没有运行

Sail artisan sail:publish
在源代码中创建一个docker文件夹。然后配置 DockerFile 进行 xdebug 安装

重新配置 docker-compose.yaml 后,运行

Sail stop
,然后运行
sail up --build
重新创建所有容器。

在launch.json中添加

pathMappings

"configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "externalConsole": false,
            "ignore": [
                "**/vendor/**/*.php"
            ],
            "pathMappings": {
                "/var/www/html": "/home/abcUser/laravelTut/firstApp/firstapp", // Map your WSL path to the container path
              },
        },

更新中: 不知道为什么,但更新 Wsl 扩展后,我在 VSCode 中获得了“端口转发”选项卡,如果我启动名为“Listen for Xdebug”的调试(如上面定义),VScode 将创建端口转发到 localhost:9003,它会生成

SAIL_XDEBUG_CONFIG:-client_host=172.26.174.72 设置不再起作用。

相反,可能需要改变两件事 在 docker-Compose.yml 中:

XDEBUG_CONFIG:'${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'

在launch.json中,添加主机名:

“主机名”:“本地主机”,


-1
投票

为什么不使用例如暴露所需的端口。

EXPOSE 9003
Dockerfile

xdebug
端口和
Dockerfile
端口需要匹配。

© www.soinside.com 2019 - 2024. All rights reserved.