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.
Answer from Yehor on Stack OverflowDebug PHP cli application inside docker with PhpStorm - Stack Overflow
php - Installing XDebug in Docker - Stack Overflow
Getting xdebug to work with my container
php with xdebug: Unable to debug console PHP in Visual Studio Code when using podman (Works in Docker)
Inside of your Docker container don't use remote_host. Also, you don't have to expose any additional ports in Docker or in Vagrant.
Here is my xdebug.ini file that works with PHP 5.6
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=0
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_connect_back=1
Make sure that PhpStorm (2016.1 in my case) is configured correctly
- Languages & Frameworks -> PHP -> Servers -> localhost -> localhost : 80 Xdebug
- Languages & Frameworks -> PHP -> Debug -> Xdebug -> Debug port: 9000
- Languages & Frameworks -> PHP -> Debug -> Xdebug -> Can accept external connections
- Languages & Frameworks -> PHP -> Debug -> DBGp Proxy -> Port 9000
Once this is done find Listen for debugger connections icon in PhpStorm in the toolbar and click it.
If you want to call it from a command line remember to include XDEBUG_SESSION cookie, i.e.
curl 'http://localhost' -sSLI -H 'Cookie: XDEBUG_SESSION=xdebug'
If you use Firefox install The easiest Xdebug and enable it in the toolbar.
In my case, debugging through web browsers worked well, the problmes came with CLI debugging (phpunit). This is because xdebug get lost with path mappings and you need to explicit tell docker.
You need to tell Docker which server configuration in PHPStorm should use, just export that env variable inside your docker container.
export PHP_IDE_CONFIG="serverName=<server-name>"
I solved this by adding the following lines into my Docker file:
# "xdebug-2.9.0" for PHP<=7.4 — "xdebug" (3) for PHP>=8
ARG XDEBUG_VERSION="xdebug-2.9.0"
FROM php:7.0-apache
RUN a2enmod rewrite
RUN docker-php-ext-install pdo pdo_mysql
RUN yes | pecl install ${XDEBUG_VERSION} \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini
COPY php.ini /usr/local/etc/php/
COPY . /var/www/html/
try this:
RUN pecl install xdebug && docker-php-ext-enable xdebug