xdebug / vscode-php-debug

PHP Debug Adapter for Visual Studio Code 🐞⛔
MIT License
763 stars 178 forks source link

Setting up debugger for php:cli docker image? #959

Closed trymeouteh closed 1 week ago

trymeouteh commented 3 months ago

PHP version: 8.3.4 Xdebug version: 3.3.1 VS Code extension version: 1.34.0

Your launch.json:

    "launch": {
        "configurations": [
            {
                "name": "PHP (Container): Terminal",
                "type": "php",
                "request": "launch",
                "pathMappings": {
                    "/app/": "${workspaceFolder}"
                }
            }
        ]
    },

Xdebug php.ini config: These are the added lines to the php.ini for xdebug

xdebug.mode = debug
xdebug.start_with_request = yes

Code snippet to reproduce:

<?php

echo 'a';
echo PHP_EOL:
echo 'b';
echo PHP_EOL:
echo 'c';

I am unable to get the debugger to work with PHP CLI when using PHP CLI inside a docker container. I would like to run php scripts inside the terminal, inside the container and debug these scripts. The reason for this is that docker allows you to run unlimited versions of PHP on your computer and there will be times one will need to use a different version of PHP.

I was able to get the debugger to work with NGINX + PHP FPM when using a PHP FPM inside a docker container and running the php scripts in the browser. I used the same debug launcher config that I shared above and the same php.ini config settings I shared above. I was able to use the debugger with the docker NGINX + PHP FPM when using the php script in the terminal and in the browser. Not sure why the same setup will not work with the PHP CLI image.

When looking for guides online, all I can find for guides using docker is for PHP FPM with NGINX for PHP web server use, not PHP CLI for terminal command line use.

Any help will be most appreciated

zobo commented 3 months ago

Add xdebug.client_host=... ip to your php.ini inside docker.

You could also try

xdebug.client_host=xdebug://gateway

You need to tell Xdebug how to reach your IDE that is on a different network outside docker. Or you need to run your docker on host network.

trymeouteh commented 2 months ago
xdebug.client_host=xdebug://gateway

This does work for docker! But I am unable to get it to work for podman.

I keep getting this error when I run the php script in the terminal with the debugger running...

$ podman container exec -it my-container bash
root@0f3710d22235:/usr/src/app# php -d xdebug.start_with_request=yes test.php
Xdebug: [Step Debug] Could not connect to debugging client. Tried: 10.0.2.2:9003 (through xdebug.client_host/xdebug.client_port, from xdebug://gateway).
zobo commented 2 months ago

You need to tell php+xdebug how to connect to your ide. I don't know podman and so I dont know what kind of network isolation is happening there. Make sure vscode listens on all interfaces (it should unless you set something specific in launch.json "host") and if you can reach that IP and port from the container. xdebug://gateway is just a helper that tries to take some well known IP from the network configuration, but it's you who knows your setup best and can set client_host to the right IP inside docker.

zobo commented 1 week ago

Closing due to no feedback