使用docker和goland断点调试代码无法成功断点

docker里面断点调试的时候断点是灰色并且无法成功断点是怎么回事?

我的docker里面的断点调试配置参照的时候jetbrain官网的指导操作的,连示例代码都是完全使用的里面的源代码。

教程链接:www.jetbrains.com/help/go/attach-t...

源代码链接:github.com/apronichev/documentatio...

但是当我按照教程里面进行配置,然后构建docker容器,最后进行断点调试的时候发现断点是显示的是灰色的,代码可以成功的在浏览器响应结果,但是无法成功断点,期待大佬门的解答,谢谢。

使用docker和goland断点调试代码无法成功断点

我的系统和ide信息如下:

  • 系统:Ubuntu 20.04.2 LTS x86_64
  • IDE: GoLand-2020.3.3
讨论数量: 2

查看了官方文档,我觉得问题在于Dockerfile
官方的docker下载了github.com/go-delve/delve/cmd/dlv包并通过dlv可执行程序吊起web服务

RUN go get github.com/go-delve/delve/cmd/dlv

COPY --from=build-env /go/bin/dlv /

CMD ["/dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", "/srv"]

Delve是Go编程语言的调试器,具体信息可在github查看
Go Remote 连接的是dlv服务监听的端口进而获取自定义程序调试信息
所以需要在docker中加入dlv并通过dlv启动自定义服务才能成功进行调试 Golang配置Go Remote窗口也有响应提示

Before running this configuration, please start your application and Delve as described bellow.  

Allow Delve to compile your application: 
dlv debug --headless --listen=:40000 --api-version=2 --accept-multiclient

 Or compile the application using Go 1.10 or newer: 
go build -gcflags \"all=-N -l\" github.com/app/demo

 and then run it via Delve with the following command: 
dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient exec ./demo.exe
3年前 评论
IzumiLight (楼主) 3年前

尝试了一下,修改了部分Dockfile文件,内容如下:

FROM golang:1.14.3-alpine3.11

ENV CGO_ENABLED 0
# 设置代理
ENV GOPROXY https://goproxy.cn,direct

# Allow Go to retreive the dependencies for the build step
RUN apk add  git
# 此步骤参考官网安装说明:https://github.com/go-delve/delve/tree/master/Documentation/installation
WORKDIR /
RUN git clone  https://github.com.cnpmjs.org/go-delve/delve.git
WORKDIR /delve
RUN go install github.com/go-delve/delve/cmd/dlv

# 复制web执行文件和配置文件,打包程序环境 GOOS=linux
COPY ./app /app
COPY ./config  /config
WORKDIR /

EXPOSE 8000 40000

CMD ["dlv", "--listen=:40000", "--headless=true", "--api-version=2","exec",  "/app"]

Go remote 配置:
file

docker启动时日志:
file
此时只是dlv监听4000端口,未启动web程序

Go remote启动时,会连接dlv并开始启动web程序
file

版本:
windows 10 Goland 2020.1.2
docker使用Docker Toolbox部署
docker –version
Docker version 18.03.0-ce, build 0520e24302

如果在Go remote执行时,docker日志并未变化,可能在dlv启动时出现了问题,可以增加参数–continue –accept-multiclient 使程序立即启动,例如:

#docker 中直接执行,此步骤需要增加端口40001映射
dlv  --continue --accept-multiclient  --listen=:40001 --headless=true  --api-version=2  exec /app
#dockerfile修改cmd
CMD ["dlv", "--continue", "--accept-multiclient","--listen=:40000", "--headless=true", "--api-version=2","exec",  "/app"]
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!