简体   繁体   English

Golang distroless Docker exec 失败:启用 CGO 时没有这样的文件或目录

[英]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错误。

google container tools 谷歌容器工具

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

相关问题 错误:fork/exec:没有这样的文件或目录——在 docker 中运行 Golang 代码时 - Error:fork/exec : no such file or directory -- when run Golang code in docker Docker exec用户进程中的Golang app导致“没有这样的文件或目录” - Golang app in Docker exec user process caused “no such file or directory” 为什么Docker运行失败? exec用户进程导致“没有这样的文件或目录” - Why Docker run failed? exec user process caused “no such file or directory” Docker exec pipe没有这样的文件或目录 - Docker exec pipe No such file or directory Golang 与 CGO inside docker 图像交叉编译 - Golang cross compiling with CGO inside docker image 使用 docker exec 将本地文件导入 docker 容器时出现“没有此类文件或目录” - "No such file or directory" when importing local file into docker container using docker exec Docker exec printf给出了没有这样的文件或目录错误 - Docker exec printf gives No such file or directory error Docker 映像中的生锈:执行没有这样的文件或目录 - Rust in Docker image: exec no such file or directory Docker golang busybox“没有这样的文件或目录”错误 - Docker golang busybox "no such file or directory" error Docker 复制失败 没有这样的文件或目录 - Docker COPY failed no such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM