Skip to content

Instantly share code, notes, and snippets.

@loru88
Last active November 29, 2023 09:44
Show Gist options
  • Save loru88/898fef61b3001a3c7cc11b5d0f3c99ce to your computer and use it in GitHub Desktop.
Save loru88/898fef61b3001a3c7cc11b5d0f3c99ce to your computer and use it in GitHub Desktop.
Manually debug PHP CLI inside a Docker container using PHPStorm

to debug a PHP cli script running inside a specific Docker container, you need to find out the the Gateway IP of the docker network your container is attached to.

grab the network name of the running container

$ docker inspect my_container | grep NetworkMode

than grab the gateway IP

$ docker network inspect my_network | grep Gateway

this value has to be used in the xdebug.remote_host propery when launching the script.

Here a little hint to automate this step:

https://tall-paul.co.uk/2018/06/06/docker-add-host-docker-internal-linux/

Get inside the container's bash and run the following for starting debugging a PHP CLI script

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host={{gateway IP}} remote_connect_back=0 idekey=PHPSTORM"
export PHP_IDE_CONFIG="serverName=localhost"
php bin/script.php

or prepend the environment variables to the command:

XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host={{gateway IP}} remote_connect_back=0 idekey=PHPSTORM" PHP_IDE_CONFIG="serverName=localhost" php bin/script.php

or if you use .env file place the environment variables

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host={{gateway IP}} remote_connect_back=0 idekey=PHPSTORM"
export PHP_IDE_CONFIG="serverName=localhost"

you need to include the export keyword otherwise the environment variable won't be inherted in the PHP script. To run it you just need to do once

$ source .env

and than run the PHP CLI script.

NOTE: If you want to place this environment variable in a .env file to be processed by for example Symfony Dotenv, you need to include the export keyword, otherwise the XDEBUG interpreter won't be able to catch it and enable the debbugging mode

in case of PHPStorm you need also:

  • same port as in File | Settings | Languages & Frameworks | PHP | Debug
  • xdebug idekey=PHPSTORM or whatever is in the File | Settings | Languages & Frameworks | PHP | Debug | DBGp Proxy
  • set env var PHP_IDE_CONFIG="serverName={{server name}}" where server name is the debug server configuration defined on the Servers page

reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment