You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用VSCode远程调试Docker容器中Go代码遇断点无法设置问题求助

VSCode远程调试Docker容器中Go代码无法设置断点,提示找不到本地文件路径

近期尝试用VSCode远程调试Docker容器内的Go代码时,遇到断点无法设置的问题:VSCode提示could not find code c:\repos\demo\main.go,但代码能正常运行,远程调试器也已成功连接。一个月前使用完全相同的操作流程时一切正常,不确定是否有环境变更导致此问题。

以下是完整操作流程:

  1. 创建本地文件夹 c:\repos\demo
  2. 在该文件夹下执行go mod init demo,生成的go.mod内容如下:
module demo
go 1.22.0
  1. 编写main.go文件:
package main
import (
    "fmt"
    "net/http"
)
func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })
    http.ListenAndServe(":8080", nil)
}
  1. 本地VSCode调试正常:可设置断点,访问http://localhost:8080能触发断点。
  2. 编写Dockerfile构建调试镜像:
FROM golang:1.22.0
WORKDIR /app
COPY . .
RUN go install github.com/go-delve/delve/cmd/dlv@latest
EXPOSE 40000 8080
CMD ["dlv", "debug", "--headless", "--listen=:40000", "--api-version=2", "--accept-multiclient", "--log"]
  1. 配置VSCode远程调试的launch.json:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Connect to server",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "port": 40000,
      "host": "127.0.0.1"
    }
  ]
}
  1. 构建并运行Docker镜像:
docker build -t demo .
docker run -p 40000:40000 -p 8080:8080 demo
  1. Docker日志显示dlv正常启动:
PS C:\repos\demo> docker run -p 40000:40000 -p 8080:8080 demo     
2024-02-16T04:46:42Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2024-02-16T04:46:42Z info layer=debugger launching process with args: [./__debug_bin4263375465]
API server listening at: [::]:40000
2024-02-16T04:46:42Z debug layer=debugger Adding target 1534 "/app/__debug_bin4263375465"
2024-02-16T04:46:50Z debug layer=debugger continuing
2024-02-16T04:46:50Z debug layer=debugger ContinueOnce
  1. 连接远程调试器后,代码正常运行,可访问http://localhost:8080得到响应,但尝试设置断点时:
    • VSCode提示:could not find code c:\repos\demo\main.go
    • Docker日志输出:
2024-02-16T04:46:59Z debug layer=debugger halting
2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "ce4e6e4ef08fa58a3535f7437bd3e592db5ac0.debug" not found in any debug info directory
2024-02-16T04:46:59Z warning layer=debugger gnu_debuglink link "e7d4a67acf053c794b3b8094e6900b5163f37d.debug" not found in any debug info directory
2024-02-16T04:46:59Z debug layer=debugger callInjection protocol on:
2024-02-16T04:46:59Z debug layer=debugger       1534 PC=0x405e8e
2024-02-16T04:46:59Z debug layer=debugger       1542 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger       1543 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger       1544 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger       1545 PC=0x4795a3
2024-02-16T04:46:59Z debug layer=debugger continuing (direction congruent)
2024-02-16T04:46:59Z debug layer=debugger ContinueOnce

内容的提问来源于stack exchange,提问作者Xuewei Han

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:24:53