6

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

enter image description here

Using phpinfo() I get following php configuration: enter image description here

enter image description here

enter image description here

And I have the following PhpStorm configuration:

Servers
enter image description here

Debug
enter image description here

DBGp Proxy (Don't really think is relevant) enter image description here

And PHP Remote Debug enter image description here

I use Chrome's Xdebug Helper plugin to send the session key enter image description here

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

enter image description here enter image description here

I am listening in the PhpStorm for Xdebug connection (with breakpoints throughout the code): enter image description here enter image description here

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: enter image description here

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Michael Chekin
  • 568
  • 4
  • 11
  • 1
    1) What is your Xdebug version? One screenshot (phpinfo) shows like it's 2.x while PhpStorm (last screenshot) shows 3.0.1. Triple check that (just in case: CLI and web server may be using different php.ini; ensure that the right URL is used etc) 2) Share whole Xdebug section of `phpinfo()` output captured via browser 3) Enable Xdebug log, try to debug and share the resulting log. **P.S.** Yes, you do not need DBGp Proxy (it's not needed for your setup at all .. and I have huge doubts that you have installed the needed proxy software at all) – LazyOne Jan 03 '21 at 12:26
  • 1
    I suggest creating some new endpoint in your actual project (I assume it serves actual web pages) and not just custom "test.php" script and call `phpinfo()` there -- this is to ensure that it is definitely served by the right server (check your ENV variables there to ensure about that). The reason I'm saying all this: Xdebug v3 uses *different* config params than v2. All important config params from v2 do *nothing* in v3. – LazyOne Jan 03 '21 at 12:32

2 Answers2

5

Thanks to LazyOne's I took another look into the configuration and found out that the Step Debugger is disabled.enter image description here

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
Michael Chekin
  • 568
  • 4
  • 11
  • 1
    Go through https://xdebug.org/docs/upgrade_guide and update your config to use Xdebug v3 params only. For every v2 param that Xdebug sees it complains about it in your error log. You do not need flooding your log with useless lines for every request, right? `xdebug.idekey=PHPSTORM` is the same in both versions, you already have 3 lines for v3 (last 3 lines). The rest `xdebug.` params can be removed or updated to v3 param names. – LazyOne Jan 03 '21 at 18:50
2

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.

Karel vIJ
  • 46
  • 3