Xdebug and CLI in Docker

Want to run a CLI command on Docker while debugging it with XDebug in an IDE like PHPStorm?
Then you need to have your environment in order.

First, create the path mappings in PHPStorm by creating a server in  Settings / Preferences | Languages & Frameworks | PHP | Servers.

Remember the name you give this server, as you’ll need it later on in an environment variable.

Then, you need to export the PHP_IDE_CONFIG environment variable in Docker in which you make a reference to server you just created in the PHPStorm configuration.

Let’s do that in one run.

We’re going to start PHPUnit in a Docker container and use XDebug to debug the session.

docker exec -it my_app \
  bash -c "export PHP_IDE_CONFIG='serverName=Docker'; phpunit"

Note that the ‘serverName’ references the name of the server you created in PHPStorm. These should match!

NOTE: If you set xdebug to start on trigger, use the following:

docker exec -it my_app \
  bash -c "export PHP_IDE_CONFIG='serverName=Docker'; XDEBUG_TRIGGER='TRUE' phpunit"

Click Here to Leave a Comment Below

Leave a Reply: