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

Docker 在单独的文件中编译带有调试符号的静态Go二进制文件?,docker,go,debugging,debug-symbols,Docker,Go,Debugging,Debug Symbols,我不记得在哪里看到的,我以为是在Datadog或NewRelic或CloudFlare上看到的?但我记得有人提到Golang,他们在生产环境中运行发行版二进制文件(当然),在Docker容器中,他们还包括一个单独的文件,其中包含调试符号,以防崩溃发生,以便能够看到发生了什么 背景 我正在Docker中构建和运行Docker文件,如下所示: # do all of our docker building in one image FROM golang:latest as build WORKD

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

背景 我正在Docker中构建和运行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"]
如果不使用上述标志,二进制文件将无法在
alpine
scratch
基本图像中执行:

standard_init_linux.go:219: exec user process caused: no such file or directory
ubuntu:latest
中运行这个功能很有效,因此上面的编译标志似乎解决了
alpine
scratch
的问题

问题:
考虑到这种环境,是否可以让
go build
将调试符号发送到单独的文件中,与Docker映像中的静态二进制文件一起使用
go tool compile
使用
-E
标记导出
调试符号。这就是你需要的吗

$ go tool compile -E *.go
类型:

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

参考:


  • 使用
    go工具编译
    使用
    -E
    标志进行
    调试符号导出
    。这就是你需要的吗

    $ go tool compile -E *.go
    
    类型:

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

    参考:

  • 当使用cgo_ENABLED=0构建时,不需要使用“-a-installsuffix cgo”标志——只需设置环境变量即可

    您正在使用“-ldflags-s”进行构建,它将删除所有调试符号和ELF符号表信息。与其这样做,不如进行常规构建,归档该可执行文件(以防以后需要符号),然后使用strip删除符号。例如

     $ CGO_ENABLED=0 GOOS=linux go build -o app.withsymbols
     $ cp app.withsymbols /my/archive/for/debugging/production/issues
     $ strip app.withsymbols -o app.stripped
     $ cp app.stripped /production/bin
    
    这应该会给出您所要求的行为(例如,一个小的生产二进制文件,还有一个带有调试生产问题符号的备份二进制文件)。

    当使用cgo_ENABLED=0构建时,您不需要使用“-a-installsuffix cgo”标志,只需设置环境变量即可

    您正在使用“-ldflags-s”进行构建,它将删除所有调试符号和ELF符号表信息。与其这样做,不如进行常规构建,归档该可执行文件(以防以后需要符号),然后使用strip删除符号。例如

     $ CGO_ENABLED=0 GOOS=linux go build -o app.withsymbols
     $ cp app.withsymbols /my/archive/for/debugging/production/issues
     $ strip app.withsymbols -o app.stripped
     $ cp app.stripped /production/bin
    
    这应该会提供您所要求的行为(例如,一个小的生产二进制文件,但也有一个备份二进制文件,带有调试生产中问题的符号)