viko
viko

Reputation: 533

Xdebug CLI in Docker

I read a lot of posts on GitHub and StackOverflow and I didn't find an answer.

I have Docker container with PHP 7.4 and Xdebug 3. The IDE is PhpStorm. When I use Xdebug in browser, Xdebug works fine. But when I use PHP CLI in container, Xdebug doesn't work.

xdebug.ini:

zend_extension=xdebug.so

[xdebug]
xdebug.mode = debug
xdebug.client_port = 9003
xdebug.discover_client_host = 1

xdebug.remote_log=/var/log/xdebug.log

I connect with bash from the container: docker exec -it CONTAINER_NAME bash and run for example: bin/console my:command (Symfony). No results, when I add to the config xdebug.start_with_request = yes and I can see the warning:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(

It looks like the container can't connect with my host system where is PhpStorm with open 9003 port. But when use the same config on webserver it works...

root@31aab3e484e1:/var/www/symfony# php -v
PHP 7.4.13 (cli) (built: Dec 11 2020 08:31:11) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans

Where is my mistake? It's php config or maybe PhpStorm?

Upvotes: 9

Views: 5609

Answers (1)

Yehor
Yehor

Reputation: 6793

Works for me on Ubuntu 20.04.

Add to you project's docker-compose.yml

(https://github.com/docker/for-linux/issues/264#issuecomment-772844305)

services:
  your_php_service_name:
    extra_hosts:
      - host.docker.internal:host-gateway

In your CLI run export XDEBUG_TRIGGER=1 && export PHP_IDE_CONFIG="serverName=host.docker.internal" to start xdebug with the request.

Thanks @LazyOne for his comment to the question with the solution.

Upvotes: 3

Related Questions