使用 VSCode 和 Docker 调试 PHP
Debug PHP with VSCode and Docker
我正在尝试使用 VSCode 在 Docker 上调试 PHP 应用程序 运行,但没有成功。
过去我能够使用 VSCode 运行 WAMP 服务器轻松调试我的 PHP 应用程序,但自从我开始使用 Docker 我无法让调试工作。在线搜索了几个教程,在 Whosebug 上检查了一些主题(例如:),但我仍然无法正常工作。
Docker文件:
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
docker-compose.yml
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 和我的 launch.json 文件,但没有任何改变。
docker-compose.yml
ports:
- 18080:80
- 14430:443
- 9000:9000 //added new xdebug default port
launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000,
"log": true
}
]
VSCode调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
更新#2:
从 docker-compose.yml 设置中删除了 Xdebug 端口 (9000)。这是 xdebug 日志结果:
Log opened at 2018-09-30 22:21:09 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:09
Log opened at 2018-09-30 22:21:17 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:17
Log opened at 2018-09-30 22:21:18 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
Log opened at 2018-09-30 22:21:18 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
还有什么建议吗?
您在 docker-compose.yml
、
中缺少端口 :9000
(或 :9001
)
需要可连接,以便 IDE 从外部连接。
对于 VSCode,可能需要 PHP Debug 扩展才能与 xdebug
交互。
默认 launch.json
仅使用 port:
9000
一次 - 并且具有 log:
true
.
{
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
}, {
"name": "Launch",
"request": "launch",
"type": "php",
"program": "${file}",
"cwd": "${workspaceRoot}",
"externalConsole": false
}
]
}
通过以下设置设法解决了我的问题:
launch.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
从 xdebug 版本 3 开始,配置名称发生了重大变化。
我当前的工作 dockerfile:
RUN apt-get update; \
apt-get -y --no-install-recommends install \
php7.4-dev \
php-pear \
libcurl3-openssl-dev \
libmcrypt-dev \
libxml2-dev \
libpng-dev \
; \
pecl install xdebug; \
{ \
echo "[xdebug]"; \
echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)"; \
echo "xdebug.mode=debug"; \
echo "xdebug.start_with_request=yes"; \
echo "xdebug.client_host=host.docker.internal"; \
echo "xdebug.client_port=9001"; \
} >> /etc/php/7.4/mods-available/xdebug.ini; \
phpenmod -v 7.4 xdebug; \
VSC 调试配置:
"configurations": [
{
"name": "XDebug (Docker)",
"type": "php",
"request": "launch",
"port": 9001,
"pathMappings": {
"/var/www/app": "${workspaceRoot}",
}
}
]
Symfony 的 XDEBUG v3 示例
docker-compose.yml:
###> XDEBUG 3 ###
# Use your client IP here
# Linux: run "ip a | grep docker0"
# Windows (with WSL2): Run "grep nameserver /etc/resolv.conf | cut -d ' ' -f2"
# MacOS: host.docker.internal
###< XDEBUG 3 ###
environment:
XDEBUG_CLIENT_HOST: 172.17.0.1
XDEBUG_CLIENT_PORT: 9003
PHP_IDE_CONFIG: serverName=Docker
volumes:
- ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
xdebug.ini
xdebug.mode=debug,coverage
xdebug.start_with_request=yes
xdebug.client_host=${XDEBUG_CLIENT_HOST}
xdebug.client_port=${XDEBUG_CLIENT_PORT}
xdebug.ini 对于 php:7.2-fpm-alpine3.8
zend_extension=xdebug.so
[xdebug]
xdebug.cli_color = 1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.client_host=host.docker.internal
xdebug.ideKey=docker
示例配置vscode修改pathMappings
launch.json:
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"log": true,
"externalConsole": false,
"pathMappings": {
"/appdata/www": "${workspaceRoot}/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
对于以下堆栈中的每个人 运行:
- Ubuntu
- X调试3
- Laravel启航
docker/8.0/php.ini
[xdebug]
xdebug.discover_client_host=true
xdebug.start_with_request=yes
.env
SAIL_XDEBUG_MODE=develop,debug
# For linux only:
# you may read up about how to get the ip here: https://laravel.com/docs/8.x/sail#debugging-with-xdebug
SAIL_XDEBUG_CONFIG="client_host=172.22.0.1"
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug via Docker",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/html": "${workspaceRoot}/www",
},
"ignore": [
"**/vendor/**/*.php"
]
},
] }
好吧,我已经尝试了以上所有答案并花了几个小时但没有成功,因为在以上所有答案中,一些概念对像我这样的新 x-debug 用户来说不清楚。我希望我在开始设置 x-调试器之前知道以下概念,运行 在 docker 容器或远程服务器上,使用我的 VS Code 的 PHP 调试。我相信这将有助于寻找此类问题的解决方案。
X-调试设置
我不确定我的机器应该被视为客户端主机还是服务器主机,虽然这很明显,但当您在配置上花费数小时后,简单的事情开始变得棘手。
xdebug.client_host
根据 X-debug 文档:
This address should be the address of the machine where your IDE or debugging client is listening for
incoming debugging connections.
我想,上面这段文字就不用解释了吧。因此,在我们的 x-debug 配置中按如下方式设置它,即 xdebug.ini:
xdebug.client_host=host.docker.internal
xdebug.remote_host
这对我来说有点棘手。实际上 x-debug 没有这样的配置,但我一直在考虑它。 :-)
PHP-调试设置
主机名
我一直在使用不同的配置,除了这个(因为在这个线程中没有人提到这个):-)但没有成功。然后我重新探索了 PHP Debug 的文档,知道在我的情况下它必须设置为 localhost
,然后试了一下,然后砰!成功了!
"hostname": "localhost"
端口
应该设置为与xdebug.client_port相同,默认值为9300。在上面的答案中重复提到。
路径映射
这是尝试使用 VS Code PHP 调试设置 docker 服务器时的另一个非常重要的配置。它是一个对象,必须将您的本地目录结构映射到 server/container 目录,即 /home/user/proj/html:${workspaceRoot}。我无法进一步解释它,它确实对我有用,我已经在这里提到了它。如果有人解释一下,我将不胜感激。
这是我的最终设置:
希望这会有所帮助并节省您的时间:-)
我正在尝试使用 VSCode 在 Docker 上调试 PHP 应用程序 运行,但没有成功。
过去我能够使用 VSCode 运行 WAMP 服务器轻松调试我的 PHP 应用程序,但自从我开始使用 Docker 我无法让调试工作。在线搜索了几个教程,在 Whosebug 上检查了一些主题(例如:
Docker文件:
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
docker-compose.yml
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 和我的 launch.json 文件,但没有任何改变。
docker-compose.yml
ports:
- 18080:80
- 14430:443
- 9000:9000 //added new xdebug default port
launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000,
"log": true
}
]
VSCode调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
更新#2: 从 docker-compose.yml 设置中删除了 Xdebug 端口 (9000)。这是 xdebug 日志结果:
Log opened at 2018-09-30 22:21:09 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:09
Log opened at 2018-09-30 22:21:17 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:17
Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
Log opened at 2018-09-30 22:21:18 I: Connecting to configured address/port: host.docker.internal:9000. E: Time-out connecting to client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
还有什么建议吗?
您在 docker-compose.yml
、
:9000
(或 :9001
)
需要可连接,以便 IDE 从外部连接。
对于 VSCode,可能需要 PHP Debug 扩展才能与 xdebug
交互。
默认 launch.json
仅使用 port:
9000
一次 - 并且具有 log:
true
.
{
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
}, {
"name": "Launch",
"request": "launch",
"type": "php",
"program": "${file}",
"cwd": "${workspaceRoot}",
"externalConsole": false
}
]
}
通过以下设置设法解决了我的问题:
launch.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
从 xdebug 版本 3 开始,配置名称发生了重大变化。
我当前的工作 dockerfile:
RUN apt-get update; \
apt-get -y --no-install-recommends install \
php7.4-dev \
php-pear \
libcurl3-openssl-dev \
libmcrypt-dev \
libxml2-dev \
libpng-dev \
; \
pecl install xdebug; \
{ \
echo "[xdebug]"; \
echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)"; \
echo "xdebug.mode=debug"; \
echo "xdebug.start_with_request=yes"; \
echo "xdebug.client_host=host.docker.internal"; \
echo "xdebug.client_port=9001"; \
} >> /etc/php/7.4/mods-available/xdebug.ini; \
phpenmod -v 7.4 xdebug; \
VSC 调试配置:
"configurations": [
{
"name": "XDebug (Docker)",
"type": "php",
"request": "launch",
"port": 9001,
"pathMappings": {
"/var/www/app": "${workspaceRoot}",
}
}
]
Symfony 的 XDEBUG v3 示例
docker-compose.yml:
###> XDEBUG 3 ###
# Use your client IP here
# Linux: run "ip a | grep docker0"
# Windows (with WSL2): Run "grep nameserver /etc/resolv.conf | cut -d ' ' -f2"
# MacOS: host.docker.internal
###< XDEBUG 3 ###
environment:
XDEBUG_CLIENT_HOST: 172.17.0.1
XDEBUG_CLIENT_PORT: 9003
PHP_IDE_CONFIG: serverName=Docker
volumes:
- ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
xdebug.ini
xdebug.mode=debug,coverage
xdebug.start_with_request=yes
xdebug.client_host=${XDEBUG_CLIENT_HOST}
xdebug.client_port=${XDEBUG_CLIENT_PORT}
xdebug.ini 对于 php:7.2-fpm-alpine3.8
zend_extension=xdebug.so
[xdebug]
xdebug.cli_color = 1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.client_host=host.docker.internal
xdebug.ideKey=docker
示例配置vscode修改pathMappings
launch.json:
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"log": true,
"externalConsole": false,
"pathMappings": {
"/appdata/www": "${workspaceRoot}/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
对于以下堆栈中的每个人 运行:
- Ubuntu
- X调试3
- Laravel启航
docker/8.0/php.ini
[xdebug] xdebug.discover_client_host=true xdebug.start_with_request=yes
.env
SAIL_XDEBUG_MODE=develop,debug # For linux only: # you may read up about how to get the ip here: https://laravel.com/docs/8.x/sail#debugging-with-xdebug SAIL_XDEBUG_CONFIG="client_host=172.22.0.1"
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Listen for XDebug via Docker", "type": "php", "request": "launch", "port": 9000, "log": true, "externalConsole": false, "pathMappings": { "/var/www/html": "${workspaceRoot}/www", }, "ignore": [ "**/vendor/**/*.php" ] }, ] }
好吧,我已经尝试了以上所有答案并花了几个小时但没有成功,因为在以上所有答案中,一些概念对像我这样的新 x-debug 用户来说不清楚。我希望我在开始设置 x-调试器之前知道以下概念,运行 在 docker 容器或远程服务器上,使用我的 VS Code 的 PHP 调试。我相信这将有助于寻找此类问题的解决方案。
X-调试设置
我不确定我的机器应该被视为客户端主机还是服务器主机,虽然这很明显,但当您在配置上花费数小时后,简单的事情开始变得棘手。
xdebug.client_host
根据 X-debug 文档:
This address should be
the address of the machine where your IDE or debugging client is listening for
incoming debugging connections.
我想,上面这段文字就不用解释了吧。因此,在我们的 x-debug 配置中按如下方式设置它,即 xdebug.ini:
xdebug.client_host=host.docker.internal
xdebug.remote_host
这对我来说有点棘手。实际上 x-debug 没有这样的配置,但我一直在考虑它。 :-)
PHP-调试设置
主机名
我一直在使用不同的配置,除了这个(因为在这个线程中没有人提到这个):-)但没有成功。然后我重新探索了 PHP Debug 的文档,知道在我的情况下它必须设置为 localhost
,然后试了一下,然后砰!成功了!
"hostname": "localhost"
端口
应该设置为与xdebug.client_port相同,默认值为9300。在上面的答案中重复提到。
路径映射
这是尝试使用 VS Code PHP 调试设置 docker 服务器时的另一个非常重要的配置。它是一个对象,必须将您的本地目录结构映射到 server/container 目录,即 /home/user/proj/html:${workspaceRoot}。我无法进一步解释它,它确实对我有用,我已经在这里提到了它。如果有人解释一下,我将不胜感激。
这是我的最终设置:
希望这会有所帮助并节省您的时间:-)