我正在使用这样的二进制文件来构建一个容器:
基本上,容器将运行一个可执行的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中看到容器,容器显示了前面的权限问题。
另外,我也不确定我是否正确地构建了这个容器。也许我需要在二进制文件中包含可执行的go程序,然后创建运行时?
任何明确的例子都将不胜感激。
发布于 2022-06-21 11:06:30
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目录的问题。(它还将为您提供更小的容器和更快的启动时间。)
https://stackoverflow.com/questions/72697986
复制相似问题