[英]Configure Xdebug in Docker container
I'm trying to create a docker container with PHP and Xdebug to use step debugging.我正在尝试使用 PHP 和 Xdebug 创建一个 docker 容器以使用分步调试。 I use VSCode and somehow this debugger it's not working.
我使用 VSCode 并且不知何故这个调试器无法正常工作。
Apparently the Dockerfile is not been executed when I use docker compose up -d
command.显然,当我使用
docker compose up -d
命令时,没有执行 Dockerfile 。 I assume that is because in the file it has a COPY
command to copy a file (called 90-xdebug.ini
) from my project to a specific directory.我认为这是因为在文件中它有一个
COPY
命令将文件(称为90-xdebug.ini
)从我的项目复制到特定目录。 And after the container is up, I check the directory and the file isn't there... then I have to execute the commands in Dockerfile manually.容器启动后,我检查目录并且文件不存在......然后我必须手动执行 Dockerfile 中的命令。
Anyways, after installing, I know that the installation worked because the xdebug_info()
function works.无论如何,安装后,我知道安装工作正常,因为
xdebug_info()
function 工作正常。 But I don't know why VSCode can't debug it.但是不知道为什么VSCode不能调试。
My OS is Ubuntu 20.04.5 LTS;我的操作系统是 Ubuntu 20.04.5 LTS; Dockerfile , docker-compose.yml and 90-xdebug.ini are in project's root.
Dockerfile 、 docker-compose.yml和90-xdebug.ini位于项目的根目录中。
My Dockerfile:我的 Dockerfile:
FROM php:7.4-apache
COPY 90-xdebug.ini "/usr/local/etc/php/conf.d"
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
90-xdebug.ini: 90-xdebug.ini:
xdebug.mode=debug
xdebug.discover_client_host=0
xdebug.client_host=host.docker.internal
docker-compose.yml: docker-compose.yml:
services:
php-apache:
container_name: php-apache
image: php:7.4-apache
build:
context: .
dockerfile: Dockerfile
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- ./:/var/www/html/
working_dir: /var/www/html/
ports:
- 3003:3003
entrypoint: "php -S 0.0.0.0:3003"
launch.json inside ".vscode" directory: “.vscode”目录中的launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
Extra information: this is my program and i've put a break point on "$var = "txt1" " line, but it runs directly towards "xdebug_info()"额外信息:这是我的程序,我在 "$var = "txt1" " 行设置了一个断点,但它直接运行到 "xdebug_info()"
<?php
function fprint(string $str):void{
echo $str;
}
$var = "txt1";
fprint($var);
$var = "txt2";
fprint($var);
$var = "txt3";
fprint($var);
// echo $PHP_INI_DIR;
// echo phpinfo();
xdebug_info();
As you say (in a comment) that xdebug_info()
shows that you had an active debugging connection, then that means that the debugger works.正如您所说(在评论中)
xdebug_info()
表明您有一个活动的调试连接,那么这意味着调试器可以工作。 What is likely happening here is that:这里可能发生的是:
you don't have any breakpoints set - in which case the debugger never breaks您没有设置任何断点 - 在这种情况下,调试器永远不会中断
more likely, that you do have a breakpoint set, but that you don't have a path mapping configured, that maps a path from inside the container ( /var/www/html
) to your local project route, which you can refer to with "${workspaceRoot}/html"
.更有可能的是,您确实设置了断点,但您没有配置路径映射,它将容器内部 (
/var/www/html
) 的路径映射到本地项目路径,您可以参考与"${workspaceRoot}/html"
。 You need to tell VS Code this path mapping by making the following configuration:您需要通过以下配置告诉 VS Code 这个路径映射:
"configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003, "pathMappings": { "/var/www/html": "${workspaceRoot}/html", } },
I might not have gotten the exact right paths in this configuration.在这个配置中我可能没有得到正确的路径。 If you tell Xdebug to make a log file (
-dxdebug.log=/tmp/xdebug.log
and -dxdebug.log_level=10
) it will create a log file in your container, where the contents tell you which breakpoint file name is tried to be matched against the files that PHP sees, something like:如果您告诉 Xdebug 创建一个日志文件(
-dxdebug.log=/tmp/xdebug.log
和-dxdebug.log_level=10
),它将在您的容器中创建一个日志文件,其中的内容告诉您尝试了哪个断点文件名与 PHP 看到的文件匹配,例如:
[11] [Step Debug] DEBUG: I: Matching breakpoint '/home/caio/dev/project/html/info.php:1' against location '/var/www/html/info.php:2'.
If these paths don't match, adjust your path mappings.如果这些路径不匹配,请调整路径映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.