我正在使用這樣的二進制檔案構建一個容器:
基本上,容器將運行一個可執行的 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 程式,然后再創建運行時?
任何明確的例子將不勝感激。
uj5u.com熱心網友回復:
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://www.uj5u.com/yidong/497294.html
標籤:码头工人 去 詹金斯 码头工人撰写 dockerfile
上一篇:失敗時暫停Jenkins作業
