1 回答

TA貢獻(xiàn)1813條經(jīng)驗 獲得超2個贊
Go 是一種編譯型語言,這意味著您實際上不需要該go工具來運行 Go 程序。在 Docker 上下文中,典型的設(shè)置是使用多階段構(gòu)建來編譯應(yīng)用程序,然后將構(gòu)建的應(yīng)用程序復(fù)制到運行它的最終映像中。最終圖像不需要 Go 工具鏈或源代碼,只需要編譯后的二進(jìn)制文件。
我可能會將最后階段重寫為:
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目錄的問題。(它還會給你一個更小的容器和更快的啟動時間。)
- 1 回答
- 0 關(guān)注
- 250 瀏覽
添加回答
舉報