1 回答

TA貢獻1813條經驗 獲得超2個贊
Go 是一種編譯型語言,這意味著您實際上不需要該go工具來運行 Go 程序。在 Docker 上下文中,典型的設置是使用多階段構建來編譯應用程序,然后將構建的應用程序復制到運行它的最終映像中。最終圖像不需要 Go 工具鏈或源代碼,只需要編譯后的二進制文件。
我可能會將最后階段重寫為:
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 關注
- 253 瀏覽
添加回答
舉報