3
votes

I've tried many things trying to let work Xdebug in a Docker container. I came in contact with these resources:

I think the problem is either something with the ports that I don't understand, or it is something with the debugger session not being started or recognized. For the debugger session I have also tried to install a browser extension that sets a cookie.

I ended up at least to have separate containers, one as dev container with enabled Xdebug.

docker-compose.yml

version: "3"
services:
  production:
    build: .
    ports:
      - "8000:80"
    volumes:
      - .:/var/www/html
  development:
    build: .
    ports:
      - "8080:80"
#      - "10000:80" also not working
    volumes:
      - .:/var/www/html
      - ./dev.php.ini:/usr/local/etc/php/php.ini

Dockerfile

FROM php:7.4.0-apache

RUN pecl install xdebug \
  && docker-php-ext-enable xdebug

dev.php.ini

xdebug.remote_enable=on
xdebug.remote_host=host.docker.internal
xdebug.remote_port=10000
xdebug.idekey=PHPSTORM

localhost:8080 phpinfo data

enter image description here

enter image description here

PhpStorm config

enter image description here

Any ideas?

1
I think your issue is the remote_host, this host is the debug client's IP, your host. And don't think host.docker.internal is being resolved as your host IP, there was a Docker feature request open for linux and think they haven't release a fix or improve this. - abestrad
1) xdebug.remote_host=host.docker.internal will work only if your host OS is Windows or Mac -- github.com/docker/for-linux/issues/264. On Linux you need to locate your host IP address yourself (e.g. ip addr and look for docker0 entry) 2) # - "10000:80" also not working -- will not work. This way you forward incoming connection from your OS into a container. But it's wrong .. as it's Xdebug that connects to IDE and NOT other way around. So it's IDE that listens on Xdebug port.. and if it's already used by Docker then no connection is possible. - LazyOne
@abestrad Actually i am using Docker Desktop (for Windows), and i saw this usage in on another working example - Fabian Picone
@LazyOne 1) I am using Docker Desktop (for Windows). 2) Thanks for this explanation. 3) Good to know there is this quickstart article, but i dont need it aymore, guess what was the solution, a PC restart. - Fabian Picone

1 Answers

2
votes

After booting up my machine to investigate further with the comments of @abestrad and @LazyOne, without changing anything, opening localhost:8080 suddenly let the debugging work by stoping at the breakpoint that i have set. Actually I had already tried to restart the Docker Desktop App before writing the question, maybe at that point my configurations were at a wrong state.

But at the end the solution was: Restarting the PC.

Watch out

To be sure i tried to open it also in a private browser session and it wasn't working anymore. That was because the special cookie still was set in the normal browser store (cookie that was stored either from the Browser extension that I have already uninstalled, or from trying out the JetBrains Bookmarklets generator before writing the question).

The solution to let it work everytime was to add following:

xdebug.remote_autostart=1

Citate from here:

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Step Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.