使用VSCode远程调试Docker容器中Go代码遇断点无法设置问题求助
VSCode远程调试Docker容器中Go代码无法设置断点,提示找不到本地文件路径
近期尝试用VSCode远程调试Docker容器内的Go代码时,遇到断点无法设置的问题:VSCode提示could not find code c:\repos\demo\main.go,但代码能正常运行,远程调试器也已成功连接。一个月前使用完全相同的操作流程时一切正常,不确定是否有环境变更导致此问题。
以下是完整操作流程:
- 创建本地文件夹
c:\repos\demo - 在该文件夹下执行
go mod init demo,生成的go.mod内容如下:
module demo go 1.22.0
- 编写
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) }
- 本地VSCode调试正常:可设置断点,访问
http://localhost:8080能触发断点。 - 编写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"]
- 配置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" } ] }
- 构建并运行Docker镜像:
docker build -t demo . docker run -p 40000:40000 -p 8080:8080 demo
- 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
- 连接远程调试器后,代码正常运行,可访问
http://localhost:8080得到响应,但尝试设置断点时:- VSCode提示:
could not find code c:\repos\demo\main.go - Docker日志输出:
- VSCode提示:
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
相关产品推荐
相关产品推荐

