Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use VScode to remote debug Go lang code in Docker container

I believe there might be an issue in this scenario. Here's what I've done:

  1. Create an empty folder c:\repos\demo
  2. In that folder I run go mod init demo, here is the mod file
    module demo
    go 1.22.0
    
  3. Add a simple main.go file in the folder
    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. Test the VScode debugging works fine, and I can put break point in my code and use browser access the http://localhost:8080 to hit my breakpoint.
  5. Add a Dockerfile to build the debugging image
    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. Add VS code remote debugging 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. Build the docker image and run it
    docker build -t demo .
    docker run -p 40000:40000 -p 8080:8080 demo
    
  8. Now I can see the dlv is running and click the VScode Connect to server to remote debug
     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. Now my code is running the remote debugger is attached. I can use my browser to hit the my "Hello, world!" end-point. However, I cannot set break point on my code, the VScode said could not find code c:\repos\demo\main.go, If click in the VScode try to set a breakpoint, the docker log said:
     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
    

I attempted to run the same code about a month ago, and everything worked perfectly. However, this time it seems to be broken. I'm not certain if anything has changed recently.

like image 873
Xuewei Han Avatar asked Jun 11 '26 09:06

Xuewei Han


1 Answers

That worked for me - open VSCode user's settings.json file and add there:

"go.delveConfig": {
    "debugAdapter": "legacy",
},
like image 101
Harduino Avatar answered Jun 14 '26 00:06

Harduino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!