【问题标题】:Xdebug can't connect back to Docker hostXdebug 无法连接回 Docker 主机
【发布时间】:2016-07-04 21:35:46
【问题描述】:

我刚刚在我的机器上设置了 Docker,并且 Nginx/PHP7 (FPM)/MySQL 设置一切正常,但是在 PHP 容器上安装了 Xdebug 我无法让它连接回我主机上的 PHPStorm机器。

这是我的 PHP Xdebug 配置...

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-
20151012/xdebug.so
xdebug.remote_log=/usr/local/var/log/xdebug.log
xdebug.remote_enable=1
xdebug.remote_host=192.168.99.1
xdebug.remote_port=9000
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true

浏览时,为容器设置了 Xdebug 启用 cookie,没有连接提示。如果我浏览一个本地托管的网站,有,所以我知道 PHPStorm 的监听是正确的。

在本地机器上,我可以 telnet 到 9000 端口……

$ telnet 192.168.99.1 9000
Trying 192.168.99.1...
Connected to 192.168.99.1.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

…但是我不能从 boot2docker 虚拟机或容器。当我尝试时,它只是坐在那里无所事事。但是,VM 和容器都可以 ping 主机。

我已经尝试禁用我的 Mac 的防火墙,但仍然没有任何乐趣。

我不太确定如何禁用 boot2docker 虚拟机上的防火墙。

非常欢迎任何关于为什么这不起作用的见解。提前致谢。

【问题讨论】:

    标签: php docker xdebug boot2docker docker-machine


    【解决方案1】:

    Xdebug 推荐的 Container 内配置:

    zend_extension = xdebug.so
    xdebug.remote_enable = 1
    xdebug.remote_connect_back = 0
    xdebug.remote_host = docker.for.mac.localhost
    xdebug.remote_port = 9000
    xdebug.remote_handler = dbgp
    xdebug.remote_mode = req
    xdebug.remote_autostart = 1
    xdebug.idekey = PHPSTORM
    

    从 Docker-17.06 开始,您可以通过静态主机名在 Container 内访问 Mac 上托管的服务:docker.for.mac.localhost

    我想从容器连接到主机上的服务?
    Mac 有一个不断变化的 IP 地址(如果您没有网络访问权限,则没有)。从 17.06 开始,我们的建议是连接到特殊的 Mac 专用 DNS 名称 docker.for.mac.localhost,它解析为主机使用的内部 IP 地址。

    https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers

    【讨论】:

    • 只是补充一点,如果有人试图在远程主机上实现这一点,您需要采取相同的方法,将remote_host设置为主机服务器IP,然后创建一个SSH隧道从本地机器到远程主机。 SSH隧道默认绑定到环回接口(你的容器不能访问),所以你需要更新你的sshd_config文件并设置GatewayPorts yes,创建你的SSH隧道,你将绑定到0.0.0.0:{port} .. xdebug 然后应该能够连接到您的 IDE
    • xdebug.remote_host = host.docker.internal 自 Docker 版本 18.03.0-ce-mac59, 2018-03-26 起工作
    【解决方案2】:

    我通过将 client_port 更改为 9000 以外的值解决了这个问题,因为我的 Mac 已经在端口 9000 上侦听了 php-fpm。

    我用它来查看列出了哪些端口以及按什么列出:

    sudo lsof -nP -iTCP -sTCP:LISTEN
    

    这就是我在容器内的 php.ini 中的 Xdebug 所需要的全部内容:

    zend_extension=xdebug.so
    xdebug.mode=debug
    xdebug.client_host=host.docker.internal
    xdebug.client_port=9009
    

    然后在 PHP 设置中调试页面的 Xdebug 部分的 PHPStorm 中的 调试端口 也设置为 9009

    【讨论】:

      【解决方案3】:

      您需要使用从 Docker 主机桥接到 Mac 的网络。在 Mac 上执行 ifconfig 并在其他本地网络上查找本地 IP,例如 10.0.1.13。 (具体细节可能因 Docker 版本而异,但这适用于 Vagrant 作为 Docker 主机,应该适用于大多数虚拟机。)

      【讨论】:

      • 感谢您的提示。我在我的无线适配器上添加了一个桥接 n/w 适配器,我可以在 docker VM 中看到一个新适配器,IP 地址为 192.168.1.18(主机为 192.168.1.6),但我仍然无法远程登录虚拟机或容器。您能否提供有关如何正确设置的更多详细信息?
      • 不应该需要另一个桥。顺便说一句,您尝试了不同的端口吗? FPM 可能正在使用 9000,因此可能会发生冲突。尝试使用 10000 或类似的值。您还可以在您的 Mac 上执行 nc -l 8888 并从容器中 telnet 以验证它是否独立于 xdebug 工作。
      • 结果重建虚拟机工作。我已经尝试了其他所有方法。最后,我删除了 VM,删除了所有仅限主机的网络,并重新创建了 VM。如果一切都失败了……重新启动!
      【解决方案4】:

      这对我有用:
      xdebug.idekey="VSCODE"
      xdebug.default_enable=1
      xdebug.remote_port=9000
      xdebug.remote_autostart=1
      xdebug.remote_enable=1
      xdebug.remote_handler=dbgp
      xdebug.profiler_enable=0
      xdebug.profiler_output_dir="/var/www/html"
      xdebug.remote_connect_back=0
      enter code here xdebug.cli_color=1
      xdebug.var_display_max_depth=10
      xdebug.remote_host= "host.docker.internal"

      我还必须从 docker-compose 中删除这些与 xdebug 相关的环境变量:

      XDEBUG_CONFIG: "remote_host=localhost"
      PHP_IDE_CONFIG: "serverName=Docker"
      来自我的 docker-compose。 这是 docker compose 的旧设置:

      如果有任何问题请告诉我:)

      【讨论】:

        猜你喜欢
        • 2018-04-27
        • 2018-01-03
        • 1970-01-01
        • 2022-11-30
        • 2019-09-29
        • 2019-06-15
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        相关资源
        最近更新 更多