Search code examples
phpdockerphpstormxdebug

Can't connect PhpStorm with xdebug with Docker


I have the following Dockerfile:

 FROM php:7.0-fpm-alpine

RUN  apk add --update --virtual build_deps gcc g++ autoconf make &&\
  docker-php-source extract &&\
  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=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_handler = dbgp" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
  echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
  docker-php-ext-enable xdebug &&\
  docker-php-source delete && \
  apk del build_deps && \
  rm -rf /var/cache/apk/* && \
  rm -rf /tmp/*

ENTRYPOINT ["php-fpm"]

And I run it with the following docker-compose.yml:

version: '2'
services:
  nginx_dev:
    image: nginx:alpine
    ports:
      - "5092:5092"
    links:
      - "my_symfony_www_dev"
    volumes:
      - './conf/nginx/nginx_dev.conf:/etc/nginx/nginx.conf:ro'
      - './logs/dev:/var/logs'
    volumes_from:
      - 'my_symfony_www_dev'

    my_symfony_www_dev:
      build:
        context: .
        dockerfile: Dockerfile_dev
      image: "myimage/my_symfony_app:dev_php"  
      volumes:
        - "$SOURCE_PATH:/var/www/html:Z"
      environment:
        - PHP_IDE_CONFIG= "serverName=my_symfony_www_dev"
        - XDEBUG_CONFIG="remote_host=10.254.254.254,remote_port=9080"
        - PHP_XDEBUG_ENABLED=1

Also as seen there I run:

sudo ip addr add 10.254.254.254/24 brd + dev enp4s0 label enp4s0:1

And configured the PhpStorm IDE like that:

Remote Debugger PhpStorm settings

Server

Xdebug Config

DBGp Proxy config

Then in Firefox I press the enter image description here button and in PhpStorm I press the enter image description here button I set a breakpoint over the app_dev.php and nothing happens.

My system listens over the port 9080 via netstat -ntlp command where the PhpStorm listens to (PhpStorm is a server according to that answer).

tcp        0      0 0.0.0.0:9080            0.0.0.0:*               LISTEN      4773/java       

Do you have any idea on why that happens and how to fix it?

Edit 1:

The phpinfo() shows:

xdebug phpinfo

I've also set de cocker-compose.yml as:

version: '2'
services:
  nginx_dev:
    image: nginx:alpine
    ports:
      - "5092:5092"
    links:
      - "my_symfony_www_dev"
    volumes:
      - './conf/nginx/nginx_dev.conf:/etc/nginx/nginx.conf:ro'
      - './logs/dev:/var/logs'
    volumes_from:
      - 'my_symfony_www_dev'

    my_symfony_www_dev:
      build:
        context: .
        dockerfile: Dockerfile_dev
      image: "myimage/my_symfony_app:dev_php"  
      volumes:
        - "$SOURCE_PATH:/var/www/html:Z"
      environment:
        - XDEBUG_CONFIG="remote_host=10.254.254.254,remote_port=9080"
        - PHP_XDEBUG_ENABLED=1

And my dockerfile is:

FROM php:7.0-fpm-alpine

RUN  apk add --update --virtual build_deps gcc g++ autoconf make &&\
  docker-php-source extract &&\
  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=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=0" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
  echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
  echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
  docker-php-ext-enable xdebug &&\
  docker-php-source delete && \
  apk del build_deps && \
  rm -rf /var/cache/apk/* && \
  rm -rf /tmp/*

ENTRYPOINT ["php-fpm"]

Still Same result.


Solution

  • In the end I had to do put the following over my Dockerfile:

    FROM php:7.0-fpm-alpine
    
    VOLUME /var/log/xdebug
    
    ARG XDEBUG_HOST="172.17.0.1"
    ARG XDEBUG_PORT=9021
    
    RUN  apk add --update --virtual build_deps gcc g++ autoconf make &&\
      docker-php-source extract &&\
      pecl install xdebug &&\
      docker-php-ext-enable xdebug &&\
      && 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_handler = dbgp" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
      echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
      echo "xdebug.remote_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
      echo "xdebug.remote_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
      docker-php-source delete && \
      apk del build_deps && \
      rm -rf /var/cache/apk/* && \
      rm -rf /tmp/*
    
    ENTRYPOINT ["php-fpm"]
    

    The XDEBUG_HOST build arg contains the ip of docker0 network interface over GNU/Linux systems (use ifconfig to find out).

    And over my docker-compose.yml I provide the following:

    version: '2'
    services:
      nginx_dev:
        image: nginx:alpine
        ports:
          - "5092:5092"
        links:
          - "my_symfony_www_dev"
        volumes:
          - './conf/nginx/nginx_dev.conf:/etc/nginx/nginx.conf:ro'
          - './logs/dev:/var/logs'
        volumes_from:
          - 'my_symfony_www_dev'
    
        my_symfony_www_dev:
          build:
            context: .
            dockerfile: Dockerfile_dev
            args:
              XDEBUG_HOST: 172.17.0.1
              XDEBUG_PORT: 9021
          image: "myimage/my_symfony_app:dev_php"  
          volumes:
            - "$SOURCE_PATH:/var/www/html:Z"
    

    Over the ./conf/nginx/nginx_dev.conf mapped over the volume I put the following setting on server section:

    server_name 0.0.0.0;
    

    Then on phpstorm use the following settings:

    Setting Servers over phpstorm

    XDEBUG settings over phpstorm

    Then you are good to go!