Golang Disteress Docker Exec失败了:启用CGO后,没有此类文件或目录

发布于 2025-02-09 16:32:09 字数 674 浏览 2 评论 0原文

我试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2025-02-16 16:32:09

您使用的图像不包含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 of gcr.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文