[英]Golang distroless Docker exec failed: No such file or directory when CGO is enabled
I was trying to get a minimal example go app running inside a docker container.我试图让一个在 docker 容器内运行的最小示例 go 应用程序。 But I kept getting
exec /app: no such file or directory
when running the container.但是我在运行容器时不断收到
exec /app: no such file or directory
。
I checked and double checked all my paths in the image where I built and copied my application data, even looked inside the container with interactive shell to verify my app
was there but nothing worked.我检查并仔细检查了构建和复制应用程序数据的映像中的所有路径,甚至使用交互式外壳查看容器内部以验证我的
app
是否存在,但没有任何效果。
My Dockerfile:我的 Dockerfile:
# syntax=docker/dockerfile:1
# BUILD-STAGE
FROM golang:1.17-alpine as build
WORKDIR /go/app
COPY . .
RUN go mod download
RUN go build -o /go/bin/app
# RUN-STAGE
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app .
EXPOSE 8080
CMD ["./app"]
After several hours of try and error I finally tried to add CGO_ENABLED=0
to my go build command.... and it worked!经过几个小时的尝试和错误,我终于尝试将
CGO_ENABLED=0
添加到我的 go build 命令中......并且它成功了!
My question is now.... why exactly does this happen?我现在的问题是......为什么会发生这种情况? There was no error when I built my image with CGO implicitly enabled and I even verified that the binary was copied into my second stage!
当我在隐式启用 CGO 的情况下构建图像时没有错误,我什至验证了二进制文件已复制到我的第二阶段! Why does the runtime say there is no such file when it was built using CGO, but can find it easily when it was built with CGO disabled?
为什么运行时在使用 CGO 构建时说没有这样的文件,但在禁用 CGO 构建时可以轻松找到它?
The image that you are using does not contain libc 1 which you build your go app against when using CGO_ENABLED=1
.您使用的图像不包含您在使用
CGO_ENABLED=1
。 As suggested in 1 you can use gcr.io/distroless/base
.正如1中所建议的,您可以使用
gcr.io/distroless/base
。 There was no error when building your app because golang:1.17-alpine contains musl libc (something like libc but smaller).构建您的应用程序时没有错误,因为 golang:1.17-alpine 包含 musl libc(类似于 libc 但更小)。 Then you tried running the app that required libc in an environment that does not have it anymore.
然后,您尝试在不再具有 libc 的环境中运行需要 libc 的应用程序。 So the
no such file
error.所以
no such file
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.