问题描述
我正在尝试使用 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
和PHP远程调试
我使用 Chrome 的 Xdebug Helper 插件发送会话密钥
在 PHPinfo()
中,我可以看到 PHP 收到了 Xdebug 会话密钥:
我在 PHPStorm 中监听 Xdebug 连接(在整个代码中带有断点):
我在启用 Xdebug Helper 的浏览器中运行应用程序。
然而。没有您期望的断点阻塞,也没有对 PHPStorm 的回调。
如果我尝试在 PHPStorm 中使用调试器配置验证,我会得到以下信息:
解决方法
多亏了 LazyOne,我再次查看了配置,发现 Step Debugger 已禁用。
我以如下方式在我的 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 有很大帮助。