在单独的文件中编译带有调试符号的静态 Go 二进制文件?

Naf*_*Kay 5 debugging debug-symbols go docker

我不记得在哪里看到过它,我以为是在 Datadog 或 NewRelic 或 CloudFlare 上?但我记得有人提到,使用 Golang,他们在生产中运行发布二进制文件(当然),并且在 Docker 容器中,他们还包含一个包含调试符号的单独文件,以防发生崩溃,以便能够看到发生了什么。

背景

我正在使用如下 Dockerfile 在 Docker 中构建和运行:

# do all of our docker building in one image
FROM golang:latest as build

WORKDIR /usr/src/api

COPY go.mod go.sum ./
RUN go mod download

COPY . .

# build the application with relevant flags to make it completely self-contained for a scratch container
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o app

# and then copy the built binary to an empty image
FROM ubuntu:latest

COPY --from=build /usr/src/api/app /
COPY --from=build /usr/src/api/config.defaults.json /config.json
COPY --from=build /usr/src/api/logo.png /

# default to running in a dev environment
ENV ENV=dev

EXPOSE 8080

ENTRYPOINT ["/bin/bash"]
Run Code Online (Sandbox Code Playgroud)

如果我不使用上面的标志,二进制文件将无法在alpine基础scratch映像中执行:

standard_init_linux.go:219: exec user process caused: no such file or directory
Run Code Online (Sandbox Code Playgroud)

运行它就可以了,所以上面的编译标志似乎解决了和 的ubuntu:latest问题。alpinescratch

问题

考虑到这种环境,是否可以将go build调试符号发送到单独的文件中,与 Docker 映像中的静态二进制文件一起存在?

p9s*_*9sh 2

使用go tool compileusing-E标志来Debug symbol export. 这是你需要的吗?

$ go tool compile -E *.go
Run Code Online (Sandbox Code Playgroud)

类型:

go tool compile
Run Code Online (Sandbox Code Playgroud)

有关如何使用它以及有哪些可用选项的更多帮助。

参考:

  1. https://golang.org/cmd/compile/