使用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
4年前 评论
IzumiLight (楼主) 4年前

尝试了一下,修改了部分 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"]
4年前 评论