I try to setup dev environment for Gin framework using docker and vscode. I can’t figure why the breakpoint is visible in the debug console but it does not have effect. In docker I’m getting successful response but breakpoint didn’t work: library-go-backend-1 | [GIN] 2024/07/21 - 10:06:11 | 200 | 440.125µs | 192.168.65.1 | GET "/"
I tried to change ports or use different options in my setup but still no clue
Here is my setup:
Dockerfile
FROM golang:1.22.1 AS builder
WORKDIR /app/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main .
#dlv
RUN CGO_ENABLED=0 go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@latest
FROM alpine:latest
WORKDIR /app/
COPY --from=builder /app/main main
COPY --from=builder /go/bin/dlv /dlv
CMD ["/dlv", "--listen=:8081", "--headless=true", "--continue", "--accept-multiclient", "--headless=true", "--api-version=2", "exec", "./main"]
docker-compose.yaml
services:
library-go-backend:
build: .
ports:
- 8080:8080
- 8081:8081
volumes:
# - ./src:/app
- ~/.dlv:/root/.dlv
.vscode/lanuch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
//"processId": 36840,
"type": "go",
"request": "attach",
"mode": "remote",
//"remotePath": "${workspaceRoot}/main.go",
"port": 8081,
"host": "127.0.0.1",
"showLog": true,
"trace": "log",
"logOutput": "rpc""
}
]
}
and the program is very simple:
package main
import (
"fmt"
"net/http"
"pbuf/models"
"github.com/gin-gonic/gin"
)
func sayOk(c *gin.Context) {
fmt.Println(2 + 2) //<-breakpoint didn't work
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
func GetDBContentTags(c *gin.Context) {
contentTags := models.ListContentTags()
c.JSON(http.StatusOK, contentTags)
}
func main() {
router := gin.Default() // <- breakpoint didn't work
router.StaticFile("/content_tag.proto", "./content_tag.proto")
//router.GET("/albums", getAlbums)
router.GET("/", sayOk)
router.GET("/content_tags", GetDBContentTags)
router.Run()
}
here are some extra logs from vscode debug console:
DisconnectRequest
Closing Delve.
Remote Debugging: close dlv connection.
DisconnectRequest to parent to shut down protocol server.
DisconnectResponse
Verbose logs are written to:
/var/folders/pl/md29lpmn1bn5cv11vk1s_wk80000gn/T/vscode-go-debug.txt
InitializeRequest
InitializeResponse
AttachRequest
Start remote debugging: connecting 127.0.0.1:8081
InitializeEvent
ConfigurationDoneRequest
ConfigurationDoneResponse {"seq":12,"type":"response","request_seq":3,"command":"configurationDone","success":true}
SetBreakPointsRequest
Halting before setting breakpoints. SkipStopEventOnce is false.
All cleared
Creating on: /Users/tomasz/pbuf/main.go:19
All set:[{"Breakpoint":{"id":60,"name":"","addr":7225507,"addrs":[7225507],"addrpid":[11],"file":"/app/main.go","line":19,"functionName":"main.GetDBContentTags","ExprString":"","Cond":"","HitCond":"","HitCondPerG":false,"continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":{},"totalHitCount":0,"disabled":false,"RootFuncName":"","TraceFollowCalls":0}}]
SetBreakPointsResponse
I send
curl localhost:8080
which results with{"status":"ok"}
curl localhost:8081
→curl: (52) Empty reply from server