【问题标题】:How to debug Golang application in Docker with VSCode?如何使用 VSCode 在 Docker 中调试 Golang 应用程序?
【发布时间】:2017-10-21 21:36:22
【问题描述】:

我正在学习在 Docker 中调试 Golang 应用程序。我在 shell 中使用 dlv connect 取得了成功。我可以添加断点,继续,下一步...我可以在 VSCode 中做笔记,但等待halting。

我在函数 main 上单击左键,直到出现红点。然后单击绿色按钮,类似于“播放”。容器中的程序运行,但不能在函数 main 上停止。

我是否以错误的方式使用 VSCode?我需要你的帮助。谢谢你。

这是我的研究图片:

#Dockerfile
FROM supinf/go:1.8-builder

RUN apk --no-cache add tini \
&& apk --no-cache add --virtual build-dependencies git \

# Compile delve
&& go get github.com/derekparker/delve/cmd/dlv \
&& cd $GOPATH/src/github.com/derekparker/delve \
&& go install github.com/derekparker/delve/cmd/dlv \

# Clean up
&& apk del --purge -r build-dependencies \
&& rm -rf /go/src/*

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["dlv", "-h"]

docker build -t mydelve .

这是我的 golang 代码:

package main

import (
    "fmt"
    "sync"
    "time"
)

func dostuff(wg *sync.WaitGroup, i int) {
    fmt.Printf("goroutine id %d\n", i)
    time.Sleep(60 * time.Second)
    fmt.Printf("end goroutine id %d\n", i)
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    workers := 10

    wg.Add(workers)
    for i := 0; i < workers; i++ {
            go dostuff(&wg, i)
    }
    wg.Wait()
}

这是运行容器的cmd:

docker run --rm -p 2345:2345 
           -v $GOPATH/src:/go/src 
           -w /go/src/test/dlv 
           --security-opt seccomp=unconfined 
           mydelve 
               dlv debug --headless --listen=:2345 --log`

连接到容器

dlv connect 127.0.0.1:2345 --wd . --log

有效。

VSCode

launch.json

{
    "version": "0.2.0",
    "configurations": [
       {
            "name": "Remote",
            "type": "go",
            "request": "launch",
            "mode": "remote",
            "program": "${fileDirname}",
            "port": 2345,
            "host": "127.0.0.1",
            "env": {},
            "args": []
        }
    ]
}

登录容器

$ docker run --rm -p 2345:2345 -v $GOPATH/src:/go/src -w /go/src/test/dlv --security-opt seccomp=unconfined mydelve dlv debug --headless --listen=:2345 --log
2017/05/22 09:07:56 server.go:73: Using API v1
2017/05/22 09:07:56 debugger.go:97: launching process with args: [/go/src/test/dlv/debug]
API server listening at: [::]:2345
2017/05/22 09:08:00 debugger.go:505: continuing
goroutine id 3
goroutine id 9
goroutine id 4
goroutine id 5
goroutine id 6
goroutine id 7
goroutine id 8
goroutine id 1
goroutine id 0
goroutine id 2
end goroutine id 6
end goroutine id 3
end goroutine id 9
end goroutine id 4
end goroutine id 5
end goroutine id 8
end goroutine id 7
end goroutine id 1
end goroutine id 0
end goroutine id 2
2017/05/22 09:08:10 debugger.go:496: halting

【问题讨论】:

    标签: docker go visual-studio-code delve


    【解决方案1】:

    我认为您需要解决的主要问题是将主机从“127.0.0.1”(localhost)更改为容器的 IP(上面的 launch.json 文件的第 11 行)。这篇文章可能会有所帮助:

    https://blog.intelligentbee.com/2016/12/15/debugging-golang-apps-in-docker-with-visual-studio-code/

    据此,“您可能必须将主机 IP 更改为您的 docker-machine ip 输出。”或者,如果您使用的是 kubernetes,则可能需要使用“kubectl describe pod”,然后查找“IP:”

    【讨论】:

    • 如果端口被暴露和映射,可以通过localhost:&lt;exposed-port&gt;访问,你不需要指向容器IP,因为每次启动容器都会改变。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2023-03-12
    • 2019-06-27
    • 2021-02-04
    • 2017-02-23
    • 2016-08-28
    • 2018-09-05
    相关资源
    最近更新 更多