简体   繁体   中英

Using Xdebug and PhpStorm with Docker container on Windows

I am trying to make Xdebug work for Docker container on Windows with PhpStorm. 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:

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: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

And I have the following PhpStorm configuration:

Servers
在此处输入图像描述

Debug
在此处输入图像描述

DBGp Proxy (Don't really think is relevant) 在此处输入图像描述

And PHP Remote Debug在此处输入图像描述

I use Chrome's Xdebug Helper plugin to send the session key在此处输入图像描述

And in phpinfo() I can see that the PHP receives the Xdebug session key:

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

I am listening in the PhpStorm for Xdebug connection (with breakpoints throughout the code): 在此处输入图像描述 在此处输入图像描述

I run the application in the browser with Xdebug Helper enabled.

Yet. There is no blocking you would expect from breakpoints and no callback to PhpStorm.

If I try to use Debugger Configuration validation in PhpStorm I get the following: 在此处输入图像描述

Thanks to LazyOne 's I took another look into the configuration and found out that the Step Debugger is disabled. 在此处输入图像描述

I install Xdebug in the following way in my php-fpm Dockerfile:

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

And this is my original Xdebug configuration:

[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:

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

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. 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). 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 . It always has the correct XDebug config and having real (https) URL's to work with helps a lot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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