php - 在VSCode中使用Docker调试PHP

标签 php docker debugging xdebug vscode-debugger

看起来一切正常,但是VSCode不会停止调试。我有一个运行PHP镜像的docker容器。

我需要做些什么才能在断点处停下来?

我看不到日志中的错误,恰恰相反,似乎他们在说断点配置正确,代码正确运行。

  • 容器的IP地址为 172.22.0.2 ,我的主机的IP地址为 172.22.0.1
  • 容器内部存在文件 xdebugo.so :/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
  • 主机 host.docker.internal 已正确设置。
  • 我使用以下图像构建图像:docker build -t phpdebug:5 .
  • 正在运行的容器中存在文件/usr/local/etc/php/conf.d/xdebug.ini

  • enter image description here

    enter image description here

    enter image description here

    launch.json
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Listen for XDebug",
                "type": "php",
                "request": "launch",
                "port": 9000,
                "log": true,
                "externalConsole": false,
                "pathMappings": {
                    "/": "${workspaceRoot}/"
                }
            }
        ]
    }
    

    docker-compose.yml
    version: '2'
    
    services:
      app:
        restart: 'no'
        image: phpdebug:5
        command: php -S 0.0.0.0:8000
        ports:
          - "8000:8000"
        volumes:
          - phpdata:/app
        environment:
          PHP_EXTENSION_XDEBUG: 1
    
    volumes:
      phpdata:
        driver: local
        driver_opts:
          type: 'none'
          o: 'bind'
          device: '/home/element/php/tuto'
    

    /usr/local/etc/php/conf.d/xdebug.ini
    zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so"
    xdebug.default_enable=1
    xdebug.remote_enable=1
    xdebug.remote_port=9000
    xdebug.remote_handler=dbgp
    xdebug.remote_connect_back=0
    xdebug.remote_host=host.docker.internal
    xdebug.idekey=VSCODE
    xdebug.remote_autostart=1
    xdebug.remote_log=/usr/local/etc/php/xdebug.log
    

    / etc / hosts
    127.0.0.1   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    172.22.0.2  d484e93eed2a
    172.22.0.1      host.docker.internal
    

    Dockerfile:
    FROM php
    
    RUN pecl install xdebug
    
    RUN ip -4 route list match 0/0 | awk '{print $3 "host.docker.internal"}' >> /etc/hosts
    
    COPY custom.ini /usr/local/etc/php/conf.d/xdebug.ini
    

    /usr/local/etc/php/xdebug.log
    [1] Log opened at 2020-02-25 03:31:45
    [1] I: Connecting to configured address/port: host.docker.internal:9000.
    [1] I: Connected to client. :-)
    [1] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///app/index.php" language="PHP" xdebug:language_version="7.4.3" protocol_version="1.0" appid="1" idekey="VSCODE"><engine version="2.9.2"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>
    
    [1] <- breakpoint_list -i 1
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="1"></response>
    
    [1] <- breakpoint_list -i 2
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="2"></response>
    
    [1] <- breakpoint_set -i 3 -t line -f file:///home/element/php/tuto/dies.php -n 2
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="3" id="10001"></response>
    
    [1] <- breakpoint_set -i 4 -t line -f file:///home/element/php/tuto/dies.php -n 10
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="4" id="10002"></response>
    
    [1] <- breakpoint_set -i 5 -t line -f file:///home/element/php/tuto/result.php -n 4
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="5" id="10003"></response>
    
    [1] <- breakpoint_list -i 6
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="6"><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="2" state="enabled" hit_count="0" hit_value="0" id="10001"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="10" state="enabled" hit_count="0" hit_value="0" id="10002"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/result.php" lineno="4" state="enabled" hit_count="0" hit_value="0" id="10003"></breakpoint></response>
    
    [1] <- breakpoint_list -i 7
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="7"><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="2" state="enabled" hit_count="0" hit_value="0" id="10001"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="10" state="enabled" hit_count="0" hit_value="0" id="10002"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/result.php" lineno="4" state="enabled" hit_count="0" hit_value="0" id="10003"></breakpoint></response>
    
    [1] <- breakpoint_set -i 8 -t exception -x *
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="8" id="10004"></response>
    
    [1] <- run -i 9
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="9" status="stopping" reason="ok"></response>
    
    [1] <- stop -i 10
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="stop" transaction_id="10" status="stopped" reason="ok"></response>
    
    [1] Log closed at 2020-02-25 03:31:45
    
    [1] Log opened at 2020-02-25 03:31:48
    [1] I: Connecting to configured address/port: host.docker.internal:9000.
    [1] I: Connected to client. :-)
    [1] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///app/dies.php" language="PHP" xdebug:language_version="7.4.3" protocol_version="1.0" appid="1" idekey="VSCODE"><engine version="2.9.2"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>
    
    [1] <- breakpoint_list -i 1
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="1"></response>
    
    [1] <- breakpoint_list -i 2
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="2"></response>
    
    [1] <- breakpoint_set -i 3 -t line -f file:///home/element/php/tuto/dies.php -n 2
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="3" id="10005"></response>
    
    [1] <- breakpoint_set -i 4 -t line -f file:///home/element/php/tuto/dies.php -n 10
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="4" id="10006"></response>
    
    [1] <- breakpoint_set -i 5 -t line -f file:///home/element/php/tuto/result.php -n 4
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="5" id="10007"></response>
    
    [1] <- breakpoint_list -i 6
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="6"><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="2" state="enabled" hit_count="0" hit_value="0" id="10005"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="10" state="enabled" hit_count="0" hit_value="0" id="10006"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/result.php" lineno="4" state="enabled" hit_count="0" hit_value="0" id="10007"></breakpoint></response>
    
    [1] <- breakpoint_list -i 7
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_list" transaction_id="7"><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="2" state="enabled" hit_count="0" hit_value="0" id="10005"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/dies.php" lineno="10" state="enabled" hit_count="0" hit_value="0" id="10006"></breakpoint><breakpoint type="line" filename="file:///home/element/php/tuto/result.php" lineno="4" state="enabled" hit_count="0" hit_value="0" id="10007"></breakpoint></response>
    
    [1] <- breakpoint_set -i 8 -t exception -x *
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="breakpoint_set" transaction_id="8" id="10008"></response>
    
    [1] <- run -i 9
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="9" status="stopping" reason="ok"></response>
    
    [1] <- stop -i 10
    [1] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="stop" transaction_id="10" status="stopped" reason="ok"></response>
    
    [1] Log closed at 2020-02-25 03:31:48
    

    vscode中的调试控制台:
    <- launchResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 2,
      command: 'launch',
      success: true
    }
    new connection 1
    <- threadEvent
    ThreadEvent {
      seq: 0,
      type: 'event',
      event: 'thread',
      body: { reason: 'started', threadId: 1 }
    }
    <- initializedEvent
    InitializedEvent { seq: 0, type: 'event', event: 'initialized' }
    -> setBreakpointsRequest
    {
      command: 'setBreakpoints',
      arguments: {
        source: { name: 'dies.php', path: '/home/element/php/tuto/dies.php' },
        lines: [ 2, 10 ],
        breakpoints: [ { line: 2 }, { line: 10 } ],
        sourceModified: false
      },
      type: 'request',
      seq: 3
    }
    -> setBreakpointsRequest
    {
      command: 'setBreakpoints',
      arguments: {
        source: { name: 'result.php', path: '/home/element/php/tuto/result.php' },
        lines: [ 4 ],
        breakpoints: [ { line: 4 } ],
        sourceModified: false
      },
      type: 'request',
      seq: 4
    }
    <- setBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 3,
      command: 'setBreakpoints',
      success: true,
      body: {
        breakpoints: [ { verified: true, line: 2 }, { verified: true, line: 10 } ]
      }
    }
    <- setBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 4,
      command: 'setBreakpoints',
      success: true,
      body: { breakpoints: [ { verified: true, line: 4 } ] }
    }
    -> setFunctionBreakpointsRequest
    {
      command: 'setFunctionBreakpoints',
      arguments: { breakpoints: [] },
      type: 'request',
      seq: 5
    }
    <- setFunctionBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 5,
      command: 'setFunctionBreakpoints',
      success: true,
      body: { breakpoints: [] }
    }
    -> setExceptionBreakpointsRequest
    {
      command: 'setExceptionBreakpoints',
      arguments: { filters: [ '*' ] },
      type: 'request',
      seq: 6
    }
    <- setExceptionBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 6,
      command: 'setExceptionBreakpoints',
      success: true
    }
    -> configurationDoneRequest
    { command: 'configurationDone', type: 'request', seq: 7 }
    <- configurationDoneResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 7,
      command: 'configurationDone',
      success: true
    }
    <- threadEvent
    ThreadEvent {
      seq: 0,
      type: 'event',
      event: 'thread',
      body: { reason: 'exited', threadId: 1 }
    }
    -> threadsRequest
    { command: 'threads', type: 'request', seq: 8 }
    <- threadsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 8,
      command: 'threads',
      success: true,
      body: { threads: [] }
    }
    -> threadsRequest
    { command: 'threads', type: 'request', seq: 9 }
    <- threadsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 9,
      command: 'threads',
      success: true,
      body: { threads: [] }
    }
    new connection 2
    <- threadEvent
    ThreadEvent {
      seq: 0,
      type: 'event',
      event: 'thread',
      body: { reason: 'started', threadId: 2 }
    }
    <- initializedEvent
    InitializedEvent { seq: 0, type: 'event', event: 'initialized' }
    -> setBreakpointsRequest
    {
      command: 'setBreakpoints',
      arguments: {
        source: { name: 'dies.php', path: '/home/element/php/tuto/dies.php' },
        lines: [ 2, 10 ],
        breakpoints: [ { line: 2 }, { line: 10 } ],
        sourceModified: false
      },
      type: 'request',
      seq: 10
    }
    -> setBreakpointsRequest
    {
      command: 'setBreakpoints',
      arguments: {
        source: { name: 'result.php', path: '/home/element/php/tuto/result.php' },
        lines: [ 4 ],
        breakpoints: [ { line: 4 } ],
        sourceModified: false
      },
      type: 'request',
      seq: 11
    }
    <- setBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 10,
      command: 'setBreakpoints',
      success: true,
      body: {
        breakpoints: [ { verified: true, line: 2 }, { verified: true, line: 10 } ]
      }
    }
    <- setBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 11,
      command: 'setBreakpoints',
      success: true,
      body: { breakpoints: [ { verified: true, line: 4 } ] }
    }
    -> setFunctionBreakpointsRequest
    {
      command: 'setFunctionBreakpoints',
      arguments: { breakpoints: [] },
      type: 'request',
      seq: 12
    }
    <- setFunctionBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 12,
      command: 'setFunctionBreakpoints',
      success: true,
      body: { breakpoints: [] }
    }
    -> setExceptionBreakpointsRequest
    {
      command: 'setExceptionBreakpoints',
      arguments: { filters: [ '*' ] },
      type: 'request',
      seq: 13
    }
    <- setExceptionBreakpointsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 13,
      command: 'setExceptionBreakpoints',
      success: true
    }
    -> configurationDoneRequest
    { command: 'configurationDone', type: 'request', seq: 14 }
    <- configurationDoneResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 14,
      command: 'configurationDone',
      success: true
    }
    <- threadEvent
    ThreadEvent {
      seq: 0,
      type: 'event',
      event: 'thread',
      body: { reason: 'exited', threadId: 2 }
    }
    -> threadsRequest
    { command: 'threads', type: 'request', seq: 15 }
    <- threadsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 15,
      command: 'threads',
      success: true,
      body: { threads: [] }
    }
    -> threadsRequest
    { command: 'threads', type: 'request', seq: 16 }
    <- threadsResponse
    Response {
      seq: 0,
      type: 'response',
      request_seq: 16,
      command: 'threads',
      success: true,
      body: { threads: [] }
    }
    
    

    我在这里尝试了答案:
    Debugging Laravel with VSCode on Docker container using Xdebug as debugger

    和这里:
    Debug PHP with VSCode and Docker

    最佳答案

    日志显示在<init数据包中:

    file:///app/index.php
    

    所有断点的配置如下:
    file:///home/element/php/tuto/result.php
    

    这表明您所做的路径映射不正确:
    "pathMappings": {
        "/": "${workspaceRoot}/"
    }
    

    在您的情况下,我相信应该是以下情况,只要这两个目录都具有index.phpresult.php文件。如果不是,则需要调整第二条路径,以便该路径具有index.phpresult.php文件。
    "pathMappings": {
        "/app": "/home/element/php/tuto"
    }
    

    关于php - 在VSCode中使用Docker调试PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60387471/

    相关文章:

    如果 Div 不可见,用于更新数据库的 PHP/JQuery/AJAX

    php - 即时添加变体并添加到购物车

    docker-compose容器在多个项目中使用了错误的容器

    docker - 如何仅推送Docker镜像中的特定更改

    debugging - 无法调试二进制文件 - "could not launch process: could not find .debug_line section in binary"

    node.js - 在 iterm 中创建触发器以打开具有匹配参数的应用程序

    php - 为什么我的 Predis 客户端的 PHPUnit 模拟不符合我的预期?

    docker - 无法连接到总线 : No such file or directory

    c - 请帮我调试我的插入排序程序

    php - 使用复选框检查要删除的列