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 关注
- 193 浏览
添加回答
举报