使用PhpStorm在docker中调试PHP cli应用程序


Debug PHP cli application inside docker with PhpStorm

我在设置调试php-cli应用程序时遇到问题。我在Mac操作系统上工作,我在这里有Vagrant和Ubuntu,在这个Ubuntu里我有docker。因此,其中一个docker容器运行我的php应用程序,php解释器就住在这里。

以前(当应用程序正好在Vagrant机器中时)我使用此命令调试cli应用程序,但现在它不起作用。:

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=192.168.10.10 remote_connect_back=0"

如何设置PhpStorm来调试我的php-cli应用程序?

在Docker容器内部不要使用remote_host。此外,您不必在Docker或Vagrant中暴露任何额外的端口。

这是我的xdebug.ini文件,它适用于PHP 5.6

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=0
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_connect_back=1

确保PhpStorm(在我的案例中为2016.1)正确配置

  • 语言&框架->PHP->服务器->localhost->localhost:80 Xdebug
  • 语言&框架->PHP->调试->Xdebug->调试端口:9000
  • 语言&框架->PHP->调试->Xdebug->可以接受外部连接
  • 语言&框架->PHP->调试->DBGp代理->端口9000

完成后,在工具栏的PhpStorm中找到侦听调试器连接图标并单击它。

如果您想从命令行调用它请记住包含XDEBUG_SESSION cookie,即

curl 'http://localhost' -sSLI -H 'Cookie: XDEBUG_SESSION=xdebug'

如果您使用Firefox,请安装最简单的Xdebug并在工具栏中启用它。

在我的案例中,通过web浏览器进行调试运行良好,问题来自CLI调试(phpunit)。这是因为xdebug丢失了路径映射,您需要显式地告诉docker。

您需要告诉DockerPHPStorm中的哪个服务器配置应该使用,只需将该env变量导出到您的Docker容器中即可。

export PHP_IDE_CONFIG="serverName=<server-name>"