提问人:BSopas 提问时间:8/17/2023 最后编辑:starballBSopas 更新时间:8/17/2023 访问量:101
使用 Docker 容器在 VSCode 中设置 Xdebug 时遇到问题
Trouble setting up Xdebug in VSCode with Docker Containers
问:
在 Docker 容器中工作时,我一直在努力在 Visual Studio Code 中为我的 PHP 项目设置 Xdebug。我已经尽了最大努力正确配置所有内容,但我遇到了一些我似乎无法解决的问题。我希望这里的社区能提供一些见解和指导。
背景:
- 我正在使用 PHP 7.4 和 Xdebug 2.9.4。
- 我已经通过phpinfo()验证了Xdebug已安装并运行。
- 当我在浏览器中激活 Xdebug 扩展时,我可以看到 Xdebug 正在侦听传入的请求。
我面临的问题:
断点无法正常工作: 主要问题是我似乎无法让断点在 Visual Studio Code 中正常工作。我已经使用 VSCode 接口设置了断点,但它们没有被触发。相反,我必须手动插入 xdebug_break();暂停执行。是什么原因导致了这种差异?
路径映射和文件导航: 当 Xdebug 在断点处停止时,它无法导航到相应的文件。我目前的项目结构如下:
C:/docker/ (base)
c:/docker/prj1
c:/docker/prj2
...
我的launch.json文件中的当前配置如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9000,
"hostname": "localhost",
"log": true,
"pathMappings": {
"/var/www/html": "${workspaceRoot}"
}
}
]
}
我的 xdebug.ini 配置如下:
zend_extension=xdebug.so
[xdebug]
xdebug.remote_port=9000
xdebug.remote_host="host.docker.internal"
xdebug.log=/var/www/html/tmp/xdebug.log
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.mode=develop,debug,trace
这是我的docker-compose.yml:
php:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
#args:
# - TZ=Europe/Lisbon
container_name: php
ports:
- 80:80
- 9003:9003
restart: unless-stopped
volumes:
- .:/var/www/html/
networks:
- app-network
extra_hosts:
- host.docker.internal:host-gateway
我也不知道为什么我的 xdebug.ini 有 xdebug.remote_host=“host.docker.internal”,并且我更改了“hostname”:“host.docker.internal”,它会停止在正确的地址侦听。
但是,当 Xdebug 在断点处停止时,它似乎会尝试使用 index.php 文件创建一个新文件夹,而不是导航到正确的位置。可能导致此问题的原因是什么?
我尝试在 Visual Studio Code 中重新安装 PHP 和 PHP 调试扩展,但无法解决这些问题。我正在与社区联系,寻求有关如何排查和解决这些问题的建议和指导。如果有人有在 Docker 化的 PHP 环境中设置 Xdebug 的经验,并且可以提供见解或解决方案,我们将不胜感激。
改变
"pathMappings": {
"/var/www/html": "${workspaceRoot}"
}
自
"pathMappings": {
"/var/www/html": "C:\\docker\\"
}
帮助解决了这个问题,现在我可以在 GUI 断点上进行调试了! 但是,我仍然想知道是什么导致了我面临的错误。
答: 暂无答案
评论
xdebug.mode=develop,debug,trace
” -- 这是针对 Xdebug v3 的,v2 中没有这样的选项。2) “ports: - 9003:9003
” -- 我假设您之前尝试过 Xdebug v3。嗯,这是错误的。如果您阅读手册:连接到客户端的是 Xdebug(在您的情况下是 VSCode),而不是相反。有了上面的内容,你就是在说“Docker,把端口9003据为己有,并转发容器内的所有传入连接”。问题是 -- 现在 VSCode 无法在该端口上侦听 Xdebug 连接。xdebug.remote_host
host.docker.internal
host.docker.internal
php_info()
"/var/www/html": "${workspaceRoot}"
/var/www/html/