20

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?

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
  • No idea what is wrong, but you should connect in your container with a `docker exec -it web_id bash` and debug inside, as you would on any Linux server with such an issue – user2915097 Jun 02 '15 at 11:30
  • are you on Windows? If yes what are you using for your Docker host? (vagrant, other?) – Thomasleveil Jun 02 '15 at 12:05
  • I'm running Ubuntu 15.04 – Christoph Grimmer Jun 02 '15 at 12:06
  • a workaround to try out: start your container with `docker run --net=host` (and remove `-p 127.0.0.1:8080:80`). This will make your container and your Docker host share the same network interface. As such from the container point-of-view, `localhost` will be the Docker host. xdebug might have less trouble figuring out how to connect to the debugger – Thomasleveil Jun 02 '15 at 12:09
  • This is not compatible with --link – Christoph Grimmer Jun 02 '15 at 12:11
  • if you start your db container also with `--net=host` you won't need link as your app would connect to `localhost:` – Thomasleveil Jun 02 '15 at 12:17
  • Works. Dammit. Thanks a lot! – Christoph Grimmer Jun 02 '15 at 12:38
  • Ok - I have no idea why this is suddenly working. But now I can run the xdebug in conjunction with -p and --link. Is simply works... – Christoph Grimmer Jun 02 '15 at 13:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79422/discussion-between-thomasleveil-and-christoph-grimmer-dietrich). – Thomasleveil Jun 02 '15 at 13:14
  • im looking to do a similar setup. what resolved it @thomasleveil – Robbo_UK Jul 04 '15 at 14:41
  • helped me: http://blog.flavia-it.de/xdebug-im-docker-container/ – Cybot Aug 27 '15 at 11:14
  • An earnest warning: XDebug messed up the program execution pretty badly. static properties of abstract classes were suddenly gone while being accessible just two lines above. It took me hours to realize that the code would run just fine with XDebug turned of :-( – Christoph Grimmer Aug 27 '15 at 11:50

5 Answers5

15

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
azerttyu
  • 151
  • 1
  • 3
  • I've tried "pecl install xdebug" and it returns an error: pecl/xdebug requires PHP (version >= 5.5.0), installed version is 5.4.45 No valid packages found install failed – Federico J. Álvarez Valero Mar 10 '17 at 10:08
  • I have the same error. I changed "FROM php:5.4-apache" to "FROM php:7.0-apache" and it resolved my issue :) – Adam Kowalski Mar 06 '18 at 20:18
  • Please read the documentation about `xdebug.remote_connect_back` it tells: If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request. It checks the $_SERVER['HTTP_X_FORWARDED_FOR'] and $_SERVER['REMOTE_ADDR'] variables to find out which IP address to use. Apparntly with it enabled the remote_host ip setting is ignored. – funder7 Oct 29 '20 at 12:21
7

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.

Roger
  • 427
  • 4
  • 13
  • 2
    host.docker.internal will only work for Docker for Windows or Docker for Mac and not available for Docker running in linux environments. https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-docker-for-mac-host-internal – bender May 15 '19 at 20:41
  • @bender this is no longer the case: https://github.com/moby/moby/pull/40007#issuecomment-578729356 – 99linesofcode Jul 25 '20 at 21:03
  • Should you be attempting to configure xdebug under WSL2, note that host.docker.internal does not point to the subsystem and requires a workaround: https://stackoverflow.com/questions/62104199/issues-when-debugging-php-in-vscode-using-docker-and-wsl2 – 99linesofcode Jul 25 '20 at 23:06
1

close: xdebug.remote_connect_back=0 add: xdebug.remote_host=192.168.0.102[your docker host/machine IP]

HamzaNig
  • 1,019
  • 1
  • 10
  • 33
0

If you don't want do change xdebug config in the container and want to make it work using xdebug.remote_connect_back=1 you can set the HTTP-Header X-Forwarded-For to the IP of host.docker.internal und thus defines PHP $_SERVER['HTTP_X_FORWARDED_FOR'] which xdebug prefers and uses as the client IP instead of $_SERVER['REMOTE_ADDR'].

paha
  • 163
  • 1
  • 7
0

Within Docker 20.10+, Xdebug works both for Ubuntu and MacOS users with this counterintuitive configuration:

xdebug.client_host=host.docker.internal
xdebug.discover_client_host=true

Tested with PhpStorm. Used to build a PHP7+ debian-based container with networking features and Xdebug capabilities, whatever the OS you run (maybe Windows as well but did not try yet).

David
  • 2,603
  • 4
  • 18
  • 28