繁体   English   中英

将 Go 服务器作为 Docker 容器运行时出现权限被拒绝错误

[英]Permission denied error when running Go server as Docker container

我想将我的 Go 服务器部署到 Google Cloud Run。 我从 指南中复制了 Dockerfile。

FROM golang:1.13 as builder

WORKDIR /app

COPY go.* ./
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -v -o server
RUN chmod a+x server

FROM alpine:3
RUN apk add --no-cache ca-certificates

COPY --from=builder /app/server /server

CMD ["/server"]

在将它部署到 Cloud Run 之前,我想通过使用docker build -t server. 并使用docker run server运行容器。

它失败并出现以下错误:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"/server\": permission denied": unknown.

谢谢你的帮助。

潜在问题 1

如果将alpine更改为debian对您有用,这意味着这是交叉编译的问题。

golang镜像基于debian,使用glibc, alpine镜像使用musl libc。 有时它们存在不兼容性,并在最坏的错误消息中暴露自己。

所以我怀疑这不是 Cloud Run 问题,而是之前的问题。 要进行验证,您还可以在本地运行容器,就像它在 Cloud Run https://cloud.google.com/run/docs/testing/local上运行一样


潜在问题 2

我曾经发生过类似的事情,结果证明我正在建造的 package 不是package main 因此,我没有生成可执行的二进制文件,而是生成了 object 文件 (.o),当然,无论我多么努力地“chmod +x”,它都不会启动。

验证您正在构建的 go package 路径实际上是package main

尝试将RUN chmod a+x添加到最终版本。

COPY --from=builder /app/server /server
RUN chmod a+x /server
CMD ["/server"]

在使用此 Firebase Cloud Run 上的GoLang 教程提供的 Dockerfile 后,我也收到了权限被拒绝的错误。

显然,这可能是因为 alpine linux 被精简到缺少容器正常构建或运行所需的关键包。

对于我的情况,它丢失了git 我将git添加到 RUN 行,就在 ca-certificates 之后以确保它已安装。 此更改后,我的 Cloud Run 实例可以正常工作。

有关详细信息,请参阅关于 Docker 库 GoLang Github 的评论

# 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"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM