前提条件
- 容器中安装了
XDebug
- 编辑器
VS Code
说明
- 并不需要在容器中额外为
xdebug
,开启端口监听 -
vscode
会开启一个监听端口, 由xdebug
主动连接 - 这里使用用
ctwj/nginx_php7
镜像
配置
1. VS Code
在调试页配置php调试环境,
会在项目根目录生成 .vscode
文件夹,
其中 launch.json 需要配置,配置如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/var/www/html": "${workspaceRoot}"
},
"log": true,
"port": 9000,
"externalConsole": false,
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
配置完成,启动调试后, 会在本机开启 9000
端口.
2. XDebug 配置
配置文件 /etc/php.d/xdebug.ini
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=docker.for.mac.localhost
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/tmp/xdebug.log
使用
在vscode
中 打断点, 尝试浏览器访问,如果代码经过断点, 会断在断点处
连接不上
- 在容器中查看
/tmp/xdebug.log
可以定位问题 - 最常见问题
xdebug.remote_host
不对, 改为实际ip地址即可
使用示例
覆盖 xdebug 配置文件
docker run -d -p 80:80 -v /website:/var/www/html -v /website/xdebug.ini:/etc/php.d/xdebug.ini ctwj/nginx_php7
附 remote_host 可选值
- docker.for.win.localhost
- docker.for.mac.localhost
- host.docker.internal
其中容器默认使用的是 docker.for.mac.localhost
网友评论