Docker Compose 和 Xdebug 在 PhpStorm 中不工作

Docker Compose and Xdebug not working in PhpStorm

我尝试了很多尝试让 Xdebug 在 Docker 容器中工作。我接触了这些资源:

我认为问题要么出在我不理解的端口上,要么出在调试器会话未启动或未被识别的问题上。对于调试器会话,我还尝试安装一个设置 cookie 的浏览器扩展。

我最终至少有了单独的容器,一个作为启用了 Xdebug 的开发容器。

docker-compose.yml

version: "3"
services:
  production:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
  development:
    build: .
    ports:
      - "8080:80"
#      - "10000:80" also not working
    volumes:
      - .:/var/www/html
      - ./dev.php.ini:/usr/local/etc/php/php.ini

Docker文件

FROM php:7.4.0-apache

RUN pecl install xdebug \
  && docker-php-ext-enable xdebug

dev.php.ini

xdebug.remote_enable=on
xdebug.remote_host=host.docker.internal
xdebug.remote_port=10000
xdebug.idekey=PHPSTORM

localhost:8080 phpinfo 数据

PhpStorm 配置

有什么想法吗?

在启动我的机器以根据@abestrad 和@LazyOne 的评论进一步调查后,不做任何更改,打开 localhost:8080 突然让调试工作停止在我设置的断点处。实际上我在写问题之前已经尝试重启Docker桌面应用程序,也许那时我的配置处于错误状态。

但最后的解决方案是:重新启动 PC

注意

为了确保我也尝试在私人浏览器会话中打开它,但它不再工作了。那是因为特殊的 cookie 仍然设置在普通的浏览器存储中(cookie 是从我已经卸载的浏览器扩展存储的,或者是在写问题之前试用 JetBrains Bookmarklets generator 存储的)。

让它每次都能正常工作的解决方案是添加以下内容:

xdebug.remote_autostart=1

引用自here

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Step Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.