使用 VScode 远程调试 Docker 容器中的 Go lang 代码

问题描述 投票:0回答:1

我认为这种情况下可能存在问题。这是我所做的:

  1. 创建一个空文件夹
    c:\repos\demo
  2. 在该文件夹中我运行
    go mod init demo
    ,这是mod文件
    module demo
    go 1.22.0
    
  3. 在文件夹中添加一个简单的
    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)
    }
    
  4. 测试 VScode 调试工作正常,我可以在代码中放置断点并使用浏览器访问
    http://localhost:8080
    来命中断点。
  5. 添加 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"]
    
  6. 添加VS code远程调试
    launch.json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Connect to server",
          "type": "go",
          "request": "attach",
          "mode": "remote",
          "port": 40000,
          "host": "127.0.0.1"
        }
      ]
    }
    
  7. 构建 docker 镜像并运行它
    docker build -t demo .
    docker run -p 40000:40000 -p 8080:8080 demo
    
  8. 现在可以看到dlv正在运行,点击VScode
    Connect to server
    进行远程调试
     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
    
  9. 现在我的代码正在运行,远程调试器已附加。我可以使用浏览器点击“Hello, world!”终点。但是,我无法在代码上设置断点,VScode 说 找不到代码 c: epos\demo\main.go,如果在VScode中点击尝试设置断点,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
    

大约一个月前,我尝试运行相同的代码,一切都运行良好。不过,这一次似乎坏了。我不确定最近是否有什么变化。

docker go debugging vscode-remote dlv
1个回答
0
投票

这对我有用 - 打开 VSCode 用户的 settings.json 文件并在其中添加:

"go.delveConfig": {
    "debugAdapter": "legacy",
},
© www.soinside.com 2019 - 2024. All rights reserved.