Debug Symfony2 in Docker with PhpStorm and Xdebug

2019-05-11 17:50发布

问题:

I'm trying to debug a Symfony app with PhpStorm by following this tutorial: https://gist.github.com/chadrien/c90927ec2d160ffea9c4

I did exactly the same but it does not work well.

php-fpm:
    build: ./php
    container_name: php-fpm-symfony
    links:
        - db
    ports:
        - 9000:9000
        - 8448:8448
        - 8000:8000
    working_dir: /var/www/html/
    volumes:
        - ../:/var/www/html
    volumes_from:
        - data
    tty: true
    env_file:
        - ./docker.env
    environment:
        XDEBUG_CONFIG: remote_host=192.168.0.176

Dockerfile

# XDEBUG
RUN yes | pecl install xdebug \
    && 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

When I'm launching the debug with PhpStorm, it seems like the connection is happening. A new window pop up in my browser. My Symfony homepage is here. But it does not stop on my breakpoint. And the step by step debug does not work.

Did I do something wrong?

回答1:

Exposing 9000 with docker-for-mac is wrong - you got the direction wrong.

The IDE, PHPstorm, listens on the port 9000, this means, it opens a socket on this 9000, it does not connect to this port.

On the other hand, xdebug connects, attaches, to the port 9000 of, usually, localhost - if it finds a listener, they speak. This, since there is no service on 9000, forwarding the port does not only not make sense, it actually harms you even, since port 9000 will be taken on OSX host localhost, and your PHPstorm will no longer be able listen on it.

What you want to do is

  1. Remove '9000:9000'
  2. Use this configuration https://gist.github.com/EugenMayer/3019516e5a3b3a01b6eac88190327e7c to a) create an alias for your OSX localhost loopback device b) configure your FPM xdebug to connect_back to this ip
  3. Now, very simple, just you press on this button https://drive.google.com/file/d/0B3SrxyqujSqxeFZoMmdrbDB6SzQ/view so it does not have this "red icon" above but it is all green. 4) Now using your browser, use an xdebug plugin as usual like https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc or Firefox alternatives, since you do not really want to use "autostart" in the xdebug config, as i suppose

Thats it, it is

  1. portable to other dev devices due to the loopback device alias
  2. you do not need to configure a debug-env, but rather use quick-debug which is, nearly in all cases, more convenient


回答2:

The other answer is correct that you have to remove the - 9000:9000 in your ports section.

The other issue is that your docker container needs to know the IP address of the docker host. This is so the correct port 9000 is connected to.

Since the release of docker 18.03, you can use the host.docker.internal DNS entry in running docker containers.

You could just add another echo line to the RUN statement that installs xdebug:

&& echo "xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/xdebug.ini \