简体   繁体   English

在 Windows 上将 Xdebug 和 PhpStorm 与 Docker 容器一起使用

[英]Using Xdebug and PhpStorm with Docker container on Windows

I am trying to make Xdebug work for Docker container on Windows with PhpStorm.我正在尝试使用 PhpStorm 使 Xdebug 为 Windows 上的 Docker 容器工作。 I read different articles and other threads, but still it's not working.我阅读了不同的文章和其他主题,但仍然无法正常工作。

Inside docker-compose.yaml I have following configuration for my app container:在 docker-compose.yaml 内部,我的应用程序容器具有以下配置:

version: "3.7"
services:

  #PHP Service
  app:
    build:
      args:
        user: user
        uid: 1000
      context: ./
      dockerfile: docker/php/Dockerfile
    image: rpg
    container_name: rpg-app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
      PHP_IDE_CONFIG: serverName=RpgServer
    working_dir: /var/www
    command: /var/www/docker/php/application-init.sh
    volumes:
      - ./:/var/www
      - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - rpg-app-network
    depends_on:
      - db

  ...

  #Nginx Service
  nginx:
    image: nginx:1.17-alpine
    container_name: rpg-nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - rpg-app-network
    depends_on:
      - app

在此处输入图像描述

Using phpinfo() I get following php configuration:使用phpinfo()我得到以下 php 配置: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

And I have the following PhpStorm configuration:我有以下 PhpStorm 配置:

Servers服务器
在此处输入图像描述

Debug调试
在此处输入图像描述

DBGp Proxy (Don't really think is relevant) DBGp 代理(真的不认为是相关的) 在此处输入图像描述

And PHP Remote Debug和PHP远程调试在此处输入图像描述

I use Chrome's Xdebug Helper plugin to send the session key我使用 Chrome 的 Xdebug Helper 插件发送 session 密钥在此处输入图像描述

And in phpinfo() I can see that the PHP receives the Xdebug session key:phpinfo()我可以看到 PHP 收到 Xdebug session 密钥:

在此处输入图像描述 在此处输入图像描述

I am listening in the PhpStorm for Xdebug connection (with breakpoints throughout the code):我正在 PhpStorm 中监听 Xdebug 连接(在整个代码中都有断点): 在此处输入图像描述 在此处输入图像描述

I run the application in the browser with Xdebug Helper enabled.我在启用了 Xdebug Helper 的浏览器中运行应用程序。

Yet.然而。 There is no blocking you would expect from breakpoints and no callback to PhpStorm.断点没有阻塞,也没有对 PhpStorm 的回调。

If I try to use Debugger Configuration validation in PhpStorm I get the following:如果我尝试在 PhpStorm 中使用调试器配置验证,我会得到以下信息: 在此处输入图像描述

Thanks to LazyOne 's I took another look into the configuration and found out that the Step Debugger is disabled.感谢LazyOne的帮助,我再次查看了配置,发现 Step Debugger 已禁用。 在此处输入图像描述

I install Xdebug in the following way in my php-fpm Dockerfile:我在我的 php-fpm Dockerfile 中以以下方式安装 Xdebug:

# Install xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug

And this is my original Xdebug configuration:这是我原来的 Xdebug 配置:

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM
xdebug.remote_log=/var/www/storage/logs/xdebug.log
xdebug.remote_mode = req

I added我添加了

xdebug.mode = debug

After rerunning docker-compose up I started receiving a Notice in container logs:重新运行docker-compose up我开始在容器日志中收到通知:

rpg-app  | NOTICE: PHP message: Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

I found this thread Xdebug: [Step Debug] Could not connect to debugging client我发现这个线程Xdebug: [Step Debug] 无法连接到调试客户端

And added:并补充说:

xdebug.client_host=host.docker.internal
xdebug.client_port=9001

Getting:得到:

[xdebug]
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM
xdebug.remote_log=/var/www/storage/logs/xdebug.log
xdebug.remote_mode = req
xdebug.mode = debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9001

Now everything works: :)现在一切正常::)

Edit: Following LazyOne's comment I updated to Xdebug v3 configuration settings.编辑:根据 LazyOne 的评论,我更新到 Xdebug v3 配置设置。 The result is:结果是:

[xdebug]

xdebug.idekey=PHPSTORM

xdebug.mode = debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9001

xdebug.log=/var/www/storage/logs/xdebug.logs

From the looks of it you have set up everything correctly except perhaps the source path mapping (you find it under servers in PHPStorm).从外观上看,除了源路径映射(您可以在 PHPStorm 的服务器下找到它)之外,您已经正确设置了所有内容。 This is often the reason breakpoints are not working.这通常是断点不起作用的原因。 Also try to enable the "Break at first line" option.还尝试启用“在第一行中断”选项。

Unless you really need a docker compose for shipping I highly recommend using Lando .除非您真的需要 docker 组合来运输,否则我强烈建议您使用Lando It always has the correct XDebug config and having real (https) URL's to work with helps a lot.它始终具有正确的 XDebug 配置,并且具有可使用的真实 (https) URL 有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM