php - docker + xdebug + 原子 : Breakpoints won't fire

标签 php docker xdebug atom-editor

我知道有很多这样的问题。不幸的是,这些答案都不适合我。

环境:

  • 主机操作系统:macOS Sierra 10.12.6
  • IDE:带有 php-debug (0.2.5) 扩展的 Atom (1.19.3)
  • php 调试设置:监听 127.0.0.1:9000,否则默认设置

Docker 文件:

FROM php:7.0-apache
RUN apt-get update \
    && apt-get install -y \
        net-tools \
    && pecl install \
        xdebug-2.5.0 \
    && docker-php-ext-enable \
        xdebug \
    && echo "ServerName localhost" >> /etc/apache2/apache2.conf \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_log=/var/log/xdebug.log" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_enable=1" >> /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_host=172.17.0.1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.idekey=xdebug-atom" >> /usr/local/etc/php/conf.d/xdebug.ini

src/index.php:

<?php
var_dump($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_ADDR']); // <-- breakpoint is set at this line. However, does not fire in IDE. var_dump() output: null, 172.17.0.1
xdebug_break(); // function is recognized, however, does not have any effect
phpinfo();

启动容器的命令:

$ docker build -t xdebug . && docker run -d -p 80:80 -v /path/to/project/src/:/var/www/html/ --name xdebug_container xdebug

在浏览器中调用“localhost”时的结果:

  • IDE 中不会触发断点,浏览器不会停止调用,而是定期加载页面。
  • 页面以 xdebug 风格显示格式化的 var_dump()。
  • phpinfo() 显示 xdebug 部分以及 Dockerfile 中的预期设置
  • Wireshark 显示 docker 容器和端口 9000 上的主机之间没有通信。
  • /var/log/xdebug.log 在容器中不存在。所以这里没有信息。
  • $ netstat -anp 没有显示容器内的 9000 端口有任何问题
  • 容器能够使用 172.17.0.1 ping 主机。

我尝试过的变体:

  • 没有指定 xdebug.idekey
  • xdebug.remote_connect_back=0 和 xdebug.remote_host 与本地主机 IP 和 127.0.0.1
  • 试过没有路径映射和路径映射:/var/www/html;/path/to/project/src
  • 不设置服务器名

背景信息: php-debug 与 MAMP Pro 的 xdebug 配合良好

phpinfo xdebug 部分:

xdebug support  enabled
Version 2.5.0
IDE Key xdebug-atom
Supported protocols Revision
DBGp - Common DeBuGger Protocol $Revision: 1.145 $
Directive   Local Value Master Value
xdebug.auto_trace   Off Off
xdebug.cli_color    0   0
xdebug.collect_assignments  Off Off
xdebug.collect_includes On  On
xdebug.collect_params   0   0
xdebug.collect_return   Off Off
xdebug.collect_vars Off Off
xdebug.coverage_enable  On  On
xdebug.default_enable   On  On
xdebug.dump.COOKIE  no value    no value
xdebug.dump.ENV no value    no value
xdebug.dump.FILES   no value    no value
xdebug.dump.GET no value    no value
xdebug.dump.POST    no value    no value
xdebug.dump.REQUEST no value    no value
xdebug.dump.SERVER  no value    no value
xdebug.dump.SESSION no value    no value
xdebug.dump_globals On  On
xdebug.dump_once    On  On
xdebug.dump_undefined   Off Off
xdebug.extended_info    On  On
xdebug.file_link_format no value    no value
xdebug.force_display_errors Off Off
xdebug.force_error_reporting    0   0
xdebug.halt_level   0   0
xdebug.idekey   xdebug-atom xdebug-atom
xdebug.max_nesting_level    256 256
xdebug.max_stack_frames -1  -1
xdebug.overload_var_dump    2   2
xdebug.profiler_aggregate   Off Off
xdebug.profiler_append  Off Off
xdebug.profiler_enable  Off Off
xdebug.profiler_enable_trigger  Off Off
xdebug.profiler_enable_trigger_value    no value    no value
xdebug.profiler_output_dir  /tmp    /tmp
xdebug.profiler_output_name cachegrind.out.%p   cachegrind.out.%p
xdebug.remote_addr_header   no value    no value
xdebug.remote_autostart On  On
xdebug.remote_connect_back  On  On
xdebug.remote_cookie_expire_time    3600    3600
xdebug.remote_enable    On  On
xdebug.remote_handler   dbgp    dbgp
xdebug.remote_host  localhost   localhost
xdebug.remote_log   /var/log/xdebug.log /var/log/xdebug.log
xdebug.remote_mode  req req
xdebug.remote_port  9000    9000
xdebug.scream   Off Off
xdebug.show_error_trace Off Off
xdebug.show_exception_trace Off Off
xdebug.show_local_vars  Off Off
xdebug.show_mem_delta   Off Off
xdebug.trace_enable_trigger Off Off
xdebug.trace_enable_trigger_value   no value    no value
xdebug.trace_format 0   0
xdebug.trace_options    0   0
xdebug.trace_output_dir /tmp    /tmp
xdebug.trace_output_name    trace.%c    trace.%c
xdebug.var_display_max_children 128 128
xdebug.var_display_max_data 512 512
xdebug.var_display_max_depth    3   3

有人知道如何处理或调试这个问题吗?

更新:

我停用了 xdebug.remote_connect_back 并将 xdebug.remote_host 设置为主机的私有(private) IP。现在主机上的 Wireshark 中有一个可见的连接。但是,容器的 TCP [SYN] 后跟主机的 TCP [RST, ACK]。似乎 Atom 不接受连接。

最佳答案

经过一些繁琐的调试我自己找到了答案:

基本问题是容器和主机设置不完全匹配。

主机上 Atom 的 php-debug 中的设置:

  • 将 IP 设置为您的本地 IP(不是 127.0.0.1!)
  • 设置路径映射

在容器上(=Dockerfile):

  • 不要设置 xdebug.idekey
  • 停用 xdebug.remote_connect_back
  • 将您的本地 IP(您在 Atom 设置中使用的 IP)设置为 xdebug.remote_host
  • 停用“docker-php-ext-enable xdebug”

关于php - docker + xdebug + 原子 : Breakpoints won't fire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878138/

相关文章:

php - XDebug 与 Aptana Studio 3

当我使用 xdebug 时出现 php 错误

php - 有没有一种简单的方法可以像使用 NGINX 的 PHP 文件一样直接使用 erb 文件?

PHP 数组到 CSV

PHP MySQL 语法问题

docker - docker run -p 3000:3000 d9a82c31eab5无法正常工作

javascript - 从 MySQL 中的 TinyMCE 数据中删除/r/n 标签

dns - 在链接的Docker容器中解析内部DNS

java - 如何将openshift中的POD日志获取到本地文件

composer-php - 将 xdebug 与 drush 命令一起使用