通过PhpStorm中的Docker容器使用xdebug

通过PhpStorm中的Docker容器使用xdebug,php,docker,phpstorm,docker-compose,xdebug,Php,Docker,Phpstorm,Docker Compose,Xdebug,我读过一些关于这方面的帖子,但没有一篇对我有帮助,也没有一篇文章忽略了缺失的部分 我无法使用Docker容器让xdebug在PhpStorm上工作 Docker compose.yml version: '2' services: web: image: nginx:latest volumes: - .:/usr/share/nginx/html - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./ngin

我读过一些关于这方面的帖子,但没有一篇对我有帮助,也没有一篇文章忽略了缺失的部分

我无法使用Docker容器让xdebug在PhpStorm上工作

Docker compose.yml

version: '2'

services:
  web:
    image: nginx:latest
    volumes:
    - .:/usr/share/nginx/html
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    - ./nginx/logs:/var/logs/nginx
    - ./nginx/site-enabled/default.conf:/etc/nginx/sites-enabled/default.conf
    ports:
    - "80:80"
    depends_on:
    - php

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 1234
      MYSQL_DATABASE: local_db
      MYSQL_USER: root
      MYSQL_PASSWORD: 1234
    ports:
    - "3306:3306"

  php:
    build: images/php
    volumes:
    - .:/usr/share/nginx/html
    - ./config/docker/php/php.ini:/usr/local/etc/php/php.ini
    - ./config/docker/php/ext-xdebug.ini:/usr/local/etc/php/conf.d/ext-xdebug.ini
    - ./config/docker/php/php-fpm.conf:/usr/local/etc/php-fpm.conf
    user: www-data
    depends_on:
    - db
config/docker/php/ext xdebug.ini

zend_extension="/usr/lib/php7/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.overload_var_dump=1
xdebug.default_enable=1
xdebug.remote_autostart=1
xdebug.idekey=PHPSTORM
xdebug.remote_connect_back=1
xdebug.remote_host=172.20.0.1 # ip of host inside docker container
xdebug.remote_log=/usr/share/nginx/html/xdebug.log
xdebug.log中的错误

Log opened at 2017-05-31 11:01:14
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.20.0.1:9000.
W: Creating socket for '172.20.0.1:9000', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2017-05-31 11:01:14
在PhpStorm中,我使用具有以下设置的远程调试器:

服务器

Host - 127.0.0.1  
Port - 80 
/usr/share/nginx/html
服务器上的绝对路径

Host - 127.0.0.1  
Port - 80 
/usr/share/nginx/html
IDE会话密钥

PHPSTORM

好的,我这里有答案

我必须将内部ip设置为
xdebug.remote\u host
并禁用
xdebug.remote\u connect\u back=0

看来这是osx的事情。
希望这对这里的人有所帮助

我发现
ext xdebug.ini
上的以下配置适用于Docker for Mac

xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
Docker在容器中自动定义主机.Docker.internal。所以我们简单地指出xdebug认为
host.docker.internal
是主机的IP(显然是)。这样,我们就不必依赖docker容器和主机之间不断变化的内部IP


更多信息可在此处找到

kill phpstorm,并在主机上检查端口9000是否被其他进程使用。如果是,您可能需要在php设置和phpstorm中更改xdebug端口。除了启动phpstorm外,没有任何进程使用此端口。如何查找内部ip?ThxIn windows,要获取内部IP,请在
cmd
中键入
ipconfig
,然后检查
DockerNat
IPI在windows 10上存在类似问题,这也是解决方案