首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dockerfile生成缓存权限问题

dockerfile生成缓存权限问题
EN

Stack Overflow用户
提问于 2022-06-21 09:00:39
回答 1查看 368关注 0票数 0

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

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

代码语言:javascript
复制
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中运行容器时,我会得到以下错误:

代码语言:javascript
复制
failed to initialize build cache at /.cache/go-build: mkdir /.cache: permission denied

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-21 11:06:30

Go是一种编译语言,这意味着您实际上不需要go工具来运行Go程序。在Docker上下文中,典型的设置是使用多级建造编译应用程序,然后将构建的应用程序复制到运行应用程序的最终映像中。最后的图像不需要Go工具链或源代码,只需要编译的二进制文件。

我可以把最后阶段改写为:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72697986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档