【问题标题】:xdebug not working in Docker Desktop for Macxdebug 在 Mac 的 Docker Desktop 中不起作用
【发布时间】:2017-01-01 19:10:59
【问题描述】:

Docker Machine 切换到 Docker Desktop for Mac 后,xdebug 停止工作。使用 xdebug 的容器无法访问主机上的端口 9000
php.ini

xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_host=172.18.0.1
xdebug.idekey=PHPSTORM

docker-compose.yml

version: '2'
services:
  php:
    image: <image name>
    ports:
      - 80:80
    # - 9000:9000
    volumes:
      - .:/var/www/html
      - ./php.ini:/usr/local/etc/php/conf.d/php.ini

xdebug.log

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.18.0.1:9000.
E: Could not connect to client. :-(

如何解决我的问题?

【问题讨论】:

    标签: macos docker xdebug docker-for-mac docker-desktop


    【解决方案1】:

    我也有同样的问题。这可能与 OSX 中 docker 的限制有关。请参阅这些链接。

    https://docs.docker.com/docker-for-mac/networking/ https://forums.docker.com/t/explain-networking-known-limitations-explain-host/15205

    还建议了可能的解决方法。其中之一是创建一个具有新 ip(例如 10.254.254.254)的设备,该设备会循环回您的 localhost。然后,当您将此 ip 用作远程主机地址而不是 docker 分配的地址(127.0.0.1 或 172.17.0.2)时,它应该可以解决问题。关注this link 获取编码解决方案

    【讨论】:

    【解决方案2】:

    将您的 docker-compose.yml 更改为以下内容。

    您需要公开端口 9000,而不是绑定。还将您的 xdebug ini 更新为主机(mac)的 ip,而不是 docker 的 ip。

    我还添加了如何将 xdebug 文件从 mac 直接挂载到 docker,以便您可以即时更新它。这使您可以进行更多控制,因为您可能必须根据从 wifi 到 wifi 的移动来更新您的 ip。 xdebug.remote_host= ip 应该是你的 mac 本地网络 ip。请记住,如果您在 apache 上执行 service apache2 restart 或适当的命令以在您更改 IP 时重新启动服务器。

    version: '2'
    services:
      php:
        image: <image name>
        ports:
          - 80:80
        expose:
          - "9000"
        volumes:
          - .:/var/www/html
          - ./php.ini:/usr/local/etc/php/conf.d/php.inivolumes:
          - ./20-xdebug.ini:/etc/php/7.1/cli/conf.d/20-xdebug.ini //obviously you would change this to your correct paths
          - ./20-xdebug.ini:/etc/php/7.1/apache2/conf.d/20-xdebug.ini //obviously you would change this to your correct paths
    
    
    # 20-xdebug.ini, this is how mine is setup. 
    zend_extension = /usr/lib/php/20160303/xdebug.so
    xdebug.remote_enable=1
    xdebug.remote_host=192.168.0.4 // Make sure you use your host (mac) local ip, not the ip of docker. 
    xdebug.remote_port=9000
    xdebug.idekey = PHPSTORM
    xdebug.remote_handler = dbgp
    xdebug.remote_autostart = 1
    xdebug.remote_log = /var/log/xdebug.log
    

    【讨论】:

      【解决方案3】:

      我已经为此苦苦挣扎了一段时间,在阅读https://docs.docker.com/docker-for-mac/networking/#httphttps-proxy-support 的官方文档后,我找到了一个更简单的解决方案 尤其是这部分:

      我想从容器连接到主机上的服务

      主机有一个不断变化的 IP 地址(如果您没有网络,则没有 使用权)。从 18.03 起,我们的建议是连接到 特殊 DNS 名称 host.docker.internal,解析为内部 主机使用的 IP 地址。这是出于开发目的,将 不适用于 Docker for Mac 之外的生产环境。

      一旦你理解了这一点,你就可以在容器内的 php.ini 中将remote_host 设置为host.docker.internal。另外不要忘记将 xdebug.remote_connect_back 设置为 0 主机设置不会被忽略:

      xdebug.remote_port=9000
      xdebug.idekey=PHPSTORM
      xdebug.remote_log=/tmp/xdebug.log
      xdebug.remote_host=host.docker.internal
      xdebug.remote_enable=1
      xdebug.remote_connect_back=0
      

      【讨论】:

        【解决方案4】:

        我使用了这个设置,它起作用了:)

        xdebug.remote_port=9000
        xdebug.idekey=PHPSTORM
        xdebug.remote_host=host.docker.internal
        xdebug.remote_enable=1
        xdebug.remote_connect_back=0
        

        在 vscode 中使用 launch.json

         "name": "Listen 9000",
         "type": "php",
         "request": "launch",
         "log": true,
         "externalConsole": false,
         "pathMappings": {
                "/var/www/html": "/Users/folder/project/src"
             },
          "port": 9000,
        

        使用 docker-compose.yml:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-10
          • 2020-07-05
          • 1970-01-01
          • 2017-08-29
          • 2019-06-07
          • 2021-10-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多