PHP xDebug works from browser but not from CLI

I’ve grown to absolutely love Docker and have several webapp containers, a DB container, ElasticSearch, etc. Everything works beautifully, including xdebug. I have a single php.ini on the container, and use a different xdebug port for each app/container. A few are Apache+mod_php, a few are Nginx+PHP-FPM - it doesn’t matter, it works fine so long as I use the browser to run the project and it appends the ?XDEBUG_SESSION_START=netbeans to the URL.

The only thing I cannot seem to figure out is - no matter what I try, I cannot make xdebug work on the command line, to debug php console commands. These files are in the same project in the same folder as my working web project, so I don’t think file mapping is the problem. I thought I might need to expose the xdebug port (eg 9000) in the docker run command, but that didn’t have any effect. Even if I start the debugger, make the connection via the browser, and then run a cli command, it never stops on debug points.

Has anyone else run into this?

in case anyone else lands here googling, answer here worked for me. (tho case was inverse)

it was related to IntelliJ option which ignores external connections from unregistered servers

Working Solution - here is my xdebug.ini

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.log_level=1
xdebug.log=/dev/null
#xdebug.profiler_enable=1
#xdebug.profiler_output_name=cachegrind.out.%u
#xdebug.profiler_output_dir="/tmp/docker-xdebug"
#xdebug.output_dir="/tmp/docker-xdebug"

In docker-compose.yml I have

      PHP_EXTENSION_XDEBUG: "1"
      PHP_IDE_CONFIG: "serverName=<your_site_local_hostname>"
      XDEBUG_SESSION: "1"

under environment stanza.

With PHPStorm, Docker-Compose & Docker I can xdebug in browser and in cli when exec’ing into the container. Pretty much the holy grail!

Enjoy -