1 回答
TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
Go 是一種編譯型語(yǔ)言,這意味著您實(shí)際上不需要該go工具來(lái)運(yùn)行 Go 程序。在 Docker 上下文中,典型的設(shè)置是使用多階段構(gòu)建來(lái)編譯應(yīng)用程序,然后將構(gòu)建的應(yīng)用程序復(fù)制到運(yùn)行它的最終映像中。最終圖像不需要 Go 工具鏈或源代碼,只需要編譯后的二進(jìn)制文件。
我可能會(huì)將最后階段重寫(xiě)為:
FROM myrepo/ubi8/go-toolset:latest AS build
# ... as you have it now ...
FROM myrepo/ubi8/ubi-minimal:latest AS runtime
# Do not install `go` in this sequence
RUN microdnf update -y --nodocs &&
microdnf install cronie -y && \
microdnf clean all
# Create a non-root user, but not a home directory;
# specific uid/gid doesn't matter
RUN adduser --system usercontainer
# Get the built binary out of the first container
# and put it somewhere in $PATH
COPY --from=build /build/build /usr/local/bin/myapp
# Switch to a non-root user and explain how to run the container
USER usercontainer
CMD ["myapp"]
此序列在最終圖像中不使用go run或不使用任何go命令,這有望解決需要$HOME/.cache目錄的問(wèn)題。(它還會(huì)給你一個(gè)更小的容器和更快的啟動(dòng)時(shí)間。)
- 1 回答
- 0 關(guān)注
- 316 瀏覽
添加回答
舉報(bào)
