Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote Debugging with XDebug from inside a Docker Container does not work

I'm trying to setup a dockered AMP environment and can't get the remote debugger working. My setup is as follows:

I have a database container running mysql which is working like a charm. I built a Docker image 'phpmysqli' with the following Dockerfile

FROM php:apache

RUN docker-php-ext-install mysqli mbstring

# zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
RUN pecl install xdebug
RUN echo 'zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so' >> /usr/local/etc/php/php.ini
RUN touch /usr/local/etc/php/conf.d/xdebug.ini; \
    echo xdebug.remote_enable=1 >> /usr/local/etc/php/conf.d/xdebug.ini; \
    echo xdebug.remote_autostart=0 >> /usr/local/etc/php/conf.d/xdebug.ini; \
    echo xdebug.remote_connect_back=1 >> /usr/local/etc/php/conf.d/xdebug.ini; \
    echo xdebug.remote_port=9000 >> /usr/local/etc/php/conf.d/xdebug.ini; \
    echo xdebug.remote_log=/tmp/php5-xdebug.log >> /usr/local/etc/php/conf.d/xdebug.ini;

RUN echo 'date.timezone = Europe/Berlin' > /usr/local/etc/php/conf.d/date.ini

I call

docker run --rm -ti  --name web -p 127.0.0.1:8080:80 -v /path/to/projects:/var/www/html --link db:db  phpmysqli

After this phpinfo respectively php -i shows that xdebug 2.3.2 is up and running.

Next I setup a Server inside IntelliJ IDEA called 'Docker' with Host 127.0.0.1, Port 8080 and Debugger Xdebug. I setup the path mapping analogous to the Volume mapping in the docker run statement.

In the PHP->Debug settings I checked that I use port 9000 for incoming connections, that I will accept external connections and that I will not ignore connections from unregistered servers.

After this I created a new PHP Remote Debug Configuration called Docker, too. Server is Docker, session id is XDEBUG_IDEA.

I can call PHP files on the Container, I can connect to the db via the link... but for some reason nothing whatsoever happens when I try to start a debug session. I tried using a cookie (and yes, I set XDEBUG_IDEA as session id in xdebug helper). I tried sending XDEBUG_SESSION_START=XDEBUG_IDEA as GET...

Can you smart people out there tell me what I missed?

like image 622
Christoph Grimmer-Dietrich Avatar asked Jun 02 '15 11:06

Christoph Grimmer-Dietrich


People also ask

How do I install Xdebug on Docker?

Install VSCode and the PHP Debug extension by Felix Becker. Build a Docker image from the official PHP image. e.g. php:7.3-apache In your image, install XDebug v3 using the Docker PHP extension installer Create a info.php that includes the line xdebug_info (); which you can access in the host's web browser to verify what Xdebug settings are enabled

What is the downside of using remote_host in Xdebug?

The downside of using xdebug.remote_host in xdebug.ini to specify our IP address is that if it our IP changes for any reason, we have to login to the container, change the xdebug.ini file and restart the container. It’s not a huge issue but when using Docker, we avoid making any changes to containers that aren’t done through configuration.

What is the default port for Xdebug 3?

XDebug 3 defaults to port 9003, while the old XDebug 2 defaults to port 9000. Watch out for this in examples on the internet. Once I got the pathMappings variable correctly setup in VSCode, I was able to step through my PHP application running in Docker!

How does Xdebug work with PHP debugger?

Depending on user action, debugger sends back some instructions to the Xdebug on the same port on which Xdebug connects with the debugger Normally you would define the configuration inside php.ini file (e.g. /etc/php/7.0/fpm/php.ini or similar).


2 Answers

You can try with this configuration. php-apache build provide two method to compile and enable module php. It's nicer to use docker-php-ext-enable xdebug to set correct file configuration.

FROM php:5.4-apache

# Enable and configure xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN sed -i '1 a xdebug.remote_autostart=true' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_mode=req' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_handler=dbgp' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_connect_back=1 ' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_port=9000' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_host=127.0.0.1' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN sed -i '1 a xdebug.remote_enable=1' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
like image 73
azerttyu Avatar answered Oct 17 '22 22:10

azerttyu


For me on a PHP, NGINX Docker environment using sublime, I got it to work with these settings:

xdebug.remote_enable = 1
xdebug.remote_mode = req
xdebug.remote_port = 9001
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal

The one that took me forever to figure out was to set the remote_host to host.docker.internal.

like image 36
Roger Avatar answered Oct 17 '22 22:10

Roger