我试图用VSCode调试运行在Docker上的PHP应用程序,但没有成功。
在过去,我可以用运行WAMP服务器的VSCode轻松调试PHP应用程序,但自从我开始使用Docker之后,我就无法让调试正常工作。在网上搜索了一些教程,在StackOverflow上查看了一些线程(例如:
Docker and XDebug not reading breakpoints VSCode
),但我还是没能让它工作。
Dockerfile文件:
FROM php:7.1.8-apache
COPY /cms /srv/app/cms
COPY .docker/cms/vhosts/vhost.conf /etc/apache2/sites-available/cms.conf
COPY .docker/cms/vhosts/vhost-ssl.conf /etc/apache2/sites-available/cms-ssl.conf
COPY .docker/cms/vhosts/certificate.conf /etc/ssl/certs/certificate.conf
COPY .docker/cms/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
WORKDIR /srv/app/cms
RUN docker-php-ext-install mbstring pdo pdo_mysql
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN chown -R www-data:www-data /srv/app/cms
RUN openssl req -x509 -new -out /etc/ssl/certs/ssl-cert-cms.crt -config /etc/ssl/certs/certificate.conf
RUN a2ensite cms.conf
RUN a2ensite cms-ssl.conf
RUN a2enmod rewrite
RUN a2enmod ssl
xdebug.ini文件
[xdebug]
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=0
xdebug.remote_host='host.docker.internal'
xdebug.idekey='VSCODE'
xdebug.remote_autostart=1
version: '3.7'
services:
cms:
build:
context: .
dockerfile: .docker/cms/Dockerfile
image: php:7.1.8-apache
ports:
- 18080:80
- 14430:443
volumes:
- ./cms:/srv/app/cms
links:
- mysql
- redis
environment:
DB_HOST: mysql
VIRTUAL_HOST: my.app.localhost
PHP_EXTENSION_XDEBUG: 1
VSCode:launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000
}, {
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
更新:
docker-compose.yml公司
ports:
- 18080:80
- 14430:443
- 9000:9000 //added new xdebug default port
启动.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000,
"log": true
}
]
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
更新#2:
从docker-compose.yml设置中删除了Xdebug端口(9000)。以下是xdebug日志结果:
日志打开时间:2018-09-30 22:21:09 I:连接到已配置的
地址/端口:host.docker。internal:9000. E:连接到超时
客户端(等待时间:200毫秒):-(日志关闭时间:2018-09-30 22:21:09
日志打开时间:2018-09-30 22:21:17 I:连接到已配置的
地址/端口:host.docker。internal:9000. E:连接到超时
客户端(等待时间:200毫秒):-(日志关闭时间:2018-09-30 22:21:17
日志打开时间:2018-09-30 22:21:18 I:连接到已配置
客户端(等待时间:200毫秒):-(日志关闭时间:2018-09-30 22:21:18
日志打开时间:2018-09-30 22:21:18 I:连接到已配置
地址/端口:host.docker。internal:9000. E:连接到超时
客户端(等待时间:200毫秒):-(日志关闭时间:2018-09-30 22:21:18
还有什么建议吗?
更新#3:使用以下设置解决了我的问题:
启动.json
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/cms",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
xdebug.ini文件
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/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=host.docker.internal
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log