dockerfile构建缓存权限问题

我正在使用这样的二进制文件构建一个容器:


基本上,容器将运行一个可执行的 go 程序。


FROM myrepo/ubi8/go-toolset:latest AS build


COPY --chown=1001:0 . /build


 RUN cd /build && \

    go env -w GO111MODULE=auto && \

    go build


#---------------------------------------------------------------

FROM myrepo/ubi8/ubi-minimal:latest AS runtime


RUN microdnf update -y --nodocs && microdnf clean all && \

    microdnf install go -y && \

    microdnf install cronie -y && \

    groupadd -g 1000 usercontainer && adduser -u 1000 -g usercontainer usercontainer && chmod 755 /home/usercontainer && \

    microdnf clean all


ENV XDG_CACHE_HOME=/home/usercontainer/.cache


COPY executable.go /tmp/executable.go


RUN chmod 0555 /tmp/executable.go


USER usercontainer

WORKDIR /home/usercontainer

但是,在 Jenkins 中运行容器时出现此错误:


failed to initialize build cache at /.cache/go-build: mkdir /.cache: permission denied

在 kubernetes 部署中手动运行容器时,我没有遇到任何问题,但 Jenkins 抛出此错误,我可以在 CrashLoopBackOff 中看到 pod,容器显示之前的权限问题。


另外,我不确定我是否正确构建了容器。也许我需要在二进制文件中包含可执行的 go 程序,然后再创建运行时?


任何明确的例子将不胜感激。


暮色呼如
浏览 215回答 1
1回答

慕姐8265434

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 sequenceRUN 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 matterRUN adduser --system usercontainer# Get the built binary out of the first container# and put it somewhere in $PATHCOPY --from=build /build/build /usr/local/bin/myapp# Switch to a non-root user and explain how to run the containerUSER usercontainerCMD ["myapp"]此序列在最终图像中不使用go run或不使用任何go命令,这有望解决需要$HOME/.cache目录的问题。(它还会给你一个更小的容器和更快的启动时间。)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go