Can't connect PhpStorm with xdebug with Docker

拜拜、爱过 提交于 2019-11-28 14:45:36

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:

Then you are good to go!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!