我不記得我在哪里看到它,我以為它是在Datadog或NewRelic上,或者CloudFlare上?但是我記得有人提到Golang,他們在生產(chǎn)中運行發(fā)布二進制文件(當然),在他們的Docker容器中,它們還包括一個單獨的文件,其中包含調試符號,以防萬一發(fā)生崩潰,以便能夠看到發(fā)生了什么。背景我正在使用這樣的Dockerfile在Docker中構建和運行:# do all of our docker building in one imageFROM golang:latest as buildWORKDIR /usr/src/apiCOPY go.mod go.sum ./RUN go mod downloadCOPY . .# build the application with relevant flags to make it completely self-contained for a scratch containerRUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o app# and then copy the built binary to an empty imageFROM ubuntu:latestCOPY --from=build /usr/src/api/app /COPY --from=build /usr/src/api/config.defaults.json /config.jsonCOPY --from=build /usr/src/api/logo.png /# default to running in a dev environmentENV ENV=devEXPOSE 8080ENTRYPOINT ["/bin/bash"]如果我不使用上面的標志,二進制文件將無法在和基本映像中執(zhí)行:alpinescratchstandard_init_linux.go:219: exec user process caused: no such file or directory運行此程序只是工作,因此上面的編譯標志似乎可以解決 和 的問題。ubuntu:latestalpinescratch問題考慮到此環(huán)境,是否可以將調試符號發(fā)出到單獨的文件中,以便與 Docker 映像中的靜態(tài)二進制文件一起存在?go build
2 回答

慕姐8265434
TA貢獻1813條經(jīng)驗 獲得超2個贊
在使用 CGO_ENABLED=0 進行構建時,您不需要使用 “ -a -installsendfix cgo” 標志 -- 只需設置環(huán)境變量即可解決問題。
您正在使用“-ldflags -s”進行構建,這將去除所有調試符號和ELF符號表信息。與其這樣做,不如進行常規(guī)構建,存檔該可執(zhí)行文件(以防以后需要符號),然后使用 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
這應該給你你所要求的行為(例如,一個小的生產(chǎn)二進制文件,但也是一個備份二進制文件,其中包含用于調試生產(chǎn)中問題的符號)。

侃侃無極
TA貢獻2051條經(jīng)驗 獲得超10個贊
使用標志到 。這是你需要的嗎?go tool compile
-E
Debug symbol export
$ go tool compile -E *.go
類型:
go tool compile
以獲取有關如何使用它以及可用選項的更多幫助。
參考:
- 2 回答
- 0 關注
- 133 瀏覽
添加回答
舉報
0/150
提交
取消