Xdebug CLI in Docker



我在GitHub和StackOverflow上读了很多帖子,但没有找到答案。

我有一个带有PHP 7.4和Xdebug 3的Docker容器。IDE是PhpStorm当我在浏览器中使用Xdebug时,Xdebug运行良好。但是,当我在容器中使用PHP CLI时,Xdebug不起作用。

xdebug.ini:

zend_extension=xdebug.so
[xdebug]
xdebug.mode = debug
xdebug.client_port = 9003
xdebug.discover_client_host = 1
xdebug.remote_log=/var/log/xdebug.log

我从容器连接bash:docker exec -it CONTAINER_NAME bash并运行例如:bin/console my:command(Symfony)。没有结果,当我添加到配置xdebug.start_with_request = yes时,我可以看到警告:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

看起来容器无法连接到我的主机系统,那里有打开的9003端口的PhpStorm。但当在Web服务器上使用相同的配置时,它可以工作。。。

root@31aab3e484e1:/var/www/symfony# php -v
PHP 7.4.13 (cli) (built: Dec 11 2020 08:31:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans

我的错误在哪里?它是php配置还是PhpStorm?

适用于Ubuntu 20.04。

添加到您项目的docker-compose.yml

(https://github.com/docker/for-linux/issues/264#issuecomment-772844305)

services:
your_php_service_name:
extra_hosts:
- host.docker.internal:host-gateway

在CLI运行中export XDEBUG_TRIGGER=1 && export PHP_IDE_CONFIG="serverName=host.docker.internal"启动带有请求的xdebug。

感谢@LazyOne对问题和解决方案的评论。

最新更新