如何在主机上访问docker容器内运行的unit testing的xdebug会话?

我有一个PHP应用程序使用hhvm在Docker容器中运行。

我的IDE是在本地主机上运行的phpstorm,我设置了xdebug。

我的/etc/hhvm/php.ini部分看起来像:

 ; xdebug hhvm.debug.server_error_message = true xdebug.enable=1 xdebug.remote_enable=1 xdebug.idekey="PHPSTORM" xdebug.remote_port = 9999 xdebug.remote_autostart=1 xdebug.remote_connect_back=1 xdebug.remote_handler=dbgp xdebug.remote_log="/var/log/xdebug/xdebug.log" 

而在我的nginx conf中,我不得不添加指定服务器名称以匹配我的反向代理。 反向代理在我的本地主机上运行,​​并将容器的IP(例如127.0.0.1:8080映射到可读的http://my_app.local

nginx conf条目看起来像这样:

 cat nginx/conf.d/my_app.conf server { listen 80 default_server; server_name my_app.local; root /var/www/my_app/web; index index.php index.html index.htm; include hhvm.conf; error_log /var/log/nginx/sandbox/error.log; access_log /var/log/nginx/sandbox/access.log main; location /status { return 200; access_log off; } location ~/(assets|img|html|src|docs|bower_components|dist)/ { try_files $uri =404; } location / { try_files $uri /app.php$is_args$args; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; } } 

当我向服务器发出请求时,这使我得到一个可用的xdebug连接。

然而,我也想要一个xdebug会话,一旦我运行的单元和functiontesting从docker容器内使用phpunit:

 le-docker-container:/var/www/my-app# ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml 

我怎样才能做到这一点?

你可以使用这种方法:

 PHP_IDE_CONFIG="serverName=my_app.local" "XDEBUG_CONFIG="remote_enable=1 remote_host=YOUR_REMOTE_IP idekey=PHPSTORM" ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml 

服务器名称必须添加到phpstorm语言和框架> PHP>服务器configurationconfiguration。 而且必须确保定义了正确的path映射。

xdebug.remote_connect_back=1需要$_SERVER['REMOTE_ADDR'] 。 尝试明确地定义它:

 le-docker-container:/var/www/my-app# REMOTE_ADDR="10.0.0.1" ./vendor/bin/phpunit -c tests/phpunit-no-coverage.xml 

或改变xdebugconfiguration来改为使用xdebug.remote_host