Golang Disteress Docker Exec失败了:启用CGO后,没有此类文件或目录
我试图在Docker容器中运行一个最小的示例GO应用程序。 但是,运行容器时,我一直在获得exec /app:没有这样的文件或目录< /code>。
我检查并仔细检查了我构建和复制应用程序数据的图像中的所有路径,甚至用交互式外壳看着容器内,以验证我的app
在那里,但无效。
我的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"]
经过几个小时的尝试和错误后,我终于尝试将cgo_enabled = 0
添加到我的go build命令...。
我的问题是现在。 当我使用CGO构建图像隐式启用时,没有错误,我甚至确认将二进制复制到我的第二阶段! 为什么运行时说使用CGO构建时没有这样的文件,但是在禁用CGO时可以轻松找到它?
I was trying to get a minimal example go app running inside a docker container.
But I kept getting exec /app: no such file or directory
when running the container.
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.
My 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!
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!
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的图像不包含libc 1 使用
cgo_enabled = 1
时,请构建您的GO应用程序。如 1 您可以使用
gcr.io /distrools/base
而不是gcr.io/distroless/static
。构建应用程序时没有错误,因为Golang:1.17-Alpine包含Musl Libc(类似于Libc,但较小)。
然后,您尝试在不再拥有它的环境中运行需要LIBC的应用程序。因此,
没有此类文件
错误。Google Container tools
The image that you are using does not contain libc 1 which you build your go app against when using
CGO_ENABLED=1
.As suggested in 1 you can use
gcr.io/distroless/base
instead ofgcr.io/distroless/static
.There was no error when building your app because golang:1.17-alpine contains musl libc (something like libc but smaller).
Then you tried running the app that required libc in an environment that does not have it anymore. So the
no such file
error.google container tools