无法将PhpStorm与xdebug与Docker连接

时间:2017-11-14 11:41:12

标签: php docker phpstorm xdebug

我有以下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"]

我使用以下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

另见there我跑:

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

并配置了PhpStorm IDE:

Remote Debugger PhpStorm settings

Server

Xdebug Config

DBGp Proxy config

然后在Firefox中我按下enter image description here按钮,在PhpStorm中我按下enter image description here按钮我在app_dev.php上设置断点,没有任何反应。

我的系统通过9080命令监听端口netstat -ntlp,其中PhpStorm监听(PhpStorm是根据that answer的服务器)。

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

您是否知道为何会发生这种情况以及如何解决这个问题?

编辑1:

phpinfo()显示:

xdebug phpinfo

我还将de cocker-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:
        - XDEBUG_CONFIG="remote_host=10.254.254.254,remote_port=9080"
        - PHP_XDEBUG_ENABLED=1

我的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=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"]

结果相同。

1 个答案:

答案 0 :(得分:2)

最后,我必须将以下内容放在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"]

XDEBUG_HOST build arg包含GNU / Linux系统上docker0网络接口的ip(使用ifconfig查找)。

在我的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
        args:
          XDEBUG_HOST: 172.17.0.1
          XDEBUG_PORT: 9021
      image: "myimage/my_symfony_app:dev_php"  
      volumes:
        - "$SOURCE_PATH:/var/www/html:Z"

./conf/nginx/nginx_dev.conf映射到卷上时,我在server部分添加了以下设置:

server_name 0.0.0.0;

然后在phpstorm上使用以下设置:

Setting Servers over phpstorm

XDEBUG settings over phpstorm

然后你很高兴去!