3 回答

TA貢獻1780條經驗 獲得超4個贊
潛在問題1
如果更改alpine
為debian
適合您,則意味著這是交叉編譯的問題。
該golang
映像基于 debian,并使用 glibc,alpine
映像使用 musl libc。有時,它們不兼容,并會出現最糟糕的錯誤消息。
所以我懷疑這不是 Cloud Run 問題,而是之前的問題。
潛在問題2
類似的事情曾經發生在我身上,結果證明我正在構建的包不是?package main
。因此,我沒有生成可執行二進制文件,而是生成了一個目標文件 (.o),當然,無論我如何努力“chmod +x”,它都不會啟動。
驗證您正在構建的 go 包路徑實際上是package main
.

TA貢獻1951條經驗 獲得超3個贊
嘗試添加RUN chmod a+x
到最終版本。
COPY --from=builder /app/server /server RUN chmod a+x /server CMD ["/server"]

TA貢獻1805條經驗 獲得超10個贊
顯然,這可能是因為 alpine linux 過于精簡,以至于缺少容器正常構建或運行所需的關鍵軟件包。
對于我的情況,它丟失了git
。我git
在 ca-certificates 之后添加到 RUN 行,以確保它已安裝。進行此更改后,我的 Cloud Run 實例可以正常運行。
# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
FROM golang:1.19 as builder
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates git
# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server
# Run the web service on container startup.
CMD ["/server"]
- 3 回答
- 0 關注
- 400 瀏覽
添加回答
舉報