在 Windows 上将 Xdebug 和 PhpStorm 与 Docker 容器一起使用

问题描述

我正在尝试使用 PHPStorm 使 Xdebug 为 Windows 上的 Docker 容器工作。我阅读了不同的文章和其他主题,但仍然无法正常工作。

在 docker-compose.yaml 中,我的应用容器有以下配置:

version: "3.7"
services:

  #PHP Service
  app:
    build:
      args:
        user: user
        uid: 1000
      context: ./
      dockerfile: docker/PHP/Dockerfile
    image: rpg
    container_name: rpg-app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
      PHP_IDE_CONfig: serverName=RpgServer
    working_dir: /var/www
    command: /var/www/docker/PHP/application-init.sh
    volumes:
      - ./:/var/www
      - ./docker/PHP/local.ini:/usr/local/etc/PHP/conf.d/local.ini
    networks:
      - rpg-app-network
    depends_on:
      - db

  ...

  #Nginx Service
  Nginx:
    image: Nginx:1.17-alpine
    container_name: rpg-Nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker/Nginx/conf.d/:/etc/Nginx/conf.d/
    networks:
      - rpg-app-network
    depends_on:
      - app

enter image description here

使用 PHPinfo() 我得到以下 PHP 配置:

enter image description here

enter image description here

enter image description here

我有以下 PHPStorm 配置:

服务器

enter image description here

调试

enter image description here

DBGp 代理(并不真正认为相关)

enter image description here

PHP远程调试

enter image description here

我使用 Chrome 的 Xdebug Helper 插件发送会话密钥

enter image description here

PHPinfo() 中,我可以看到 PHP 收到了 Xdebug 会话密钥:

enter image description here

enter image description here

我在 PHPStorm 中监听 Xdebug 连接(在整个代码中带有断点):

enter image description here

enter image description here

我在启用 Xdebug Helper 的浏览器中运行应用程序。

然而。没有您期望的断点阻塞,也没有对 PHPStorm 的回调。

如果我尝试在 PHPStorm 中使用调试器配置验证,我会得到以下信息:

enter image description here

解决方法

多亏了 LazyOne,我再次查看了配置,发现 Step Debugger 已禁用。enter image description here

我以如下方式在我的 php-fpm Dockerfile 中安装 Xdebug:

# Install xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug

这是我原来的 Xdebug 配置:

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM
xdebug.remote_log=/var/www/storage/logs/xdebug.log
xdebug.remote_mode = req

我添加了

xdebug.mode = debug

重新运行 docker-compose up 后,我开始在容器日志中收到通知:

rpg-app  | NOTICE: PHP message: Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

我找到了这个话题 Xdebug: [Step Debug] Could not connect to debugging client

并补充:

xdebug.client_host=host.docker.internal
xdebug.client_port=9001

获取:

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM
xdebug.remote_log=/var/www/storage/logs/xdebug.log
xdebug.remote_mode = req
xdebug.mode = debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9001

现在一切正常! :)

编辑: 在 LazyOne 的 comment 之后,我更新了 Xdebug v3 配置设置。 结果是:

[xdebug]

xdebug.idekey=PHPSTORM

xdebug.mode = debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9001

xdebug.log=/var/www/storage/logs/xdebug.logs
,

从它的外观来看,除了源路径映射之外,您已经正确设置了所有内容(您可以在 PHPStorm 的服务器下找到它)。这通常是断点不起作用的原因。也尝试启用“在第一行中断”选项。

除非你真的需要一个 docker compose 来发送,否则我强烈推荐使用 Lando。 它始终具有正确的 XDebug 配置,并且具有可使用的真实 (https) URL 有很大帮助。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...