Docker + xdebug + Atom:断点不会触发

时间:2017-08-25 09:14:13

标签: php docker xdebug atom-editor

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

环境:

  • 主机操作系统:macOS Sierra 10.12.6
  • IDE:使用php-debug(0.2.5)扩展名的Atom(1.19.3)
  • php-debug设置:侦听127.0.0.1:9000,否则默认设置

Dockerfile:

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显示在端口9000上的docker容器和主机之间没有进行通信。
  • /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
  • 未设置ServerName

背景信息 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设置为我主机的私有IP。现在主机上的Wireshark中显示了一个连接。但是,容器的TCP [SYN]后面跟着主机的TCP [RST,ACK]。似乎Atom不接受连接。

1 个答案:

答案 0 :(得分:1)

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

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

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

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

在容器(= Dockerfile)上:

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