为什么将多阶段 Docker 构建中的基础镜像从 distroless 切换到 alpine 会导致“exec user process Caused: no such file or directory”?

发布于 2025-01-10 01:36:33 字数 1293 浏览 3 评论 0原文

我正在遵循 https://docs.docker.com/language/ 上的指南golang/build-images/ 了解为 Go 应用程序进行多阶段 Docker 构建的最佳方法。我克隆了存储库:

git clone https://github.com/olliefr/docker-gs-ping

并且我在指南底部附近运行了命令来构建图像:

docker build -t docker-gs-ping:multistage -f Dockerfile.multistage .

我还运行了自己的命令来从图像运行临时容器:

docker run --rm docker-gs-ping:multistage

这工作得很好。我在终端中看到应用程序的输出。但是,我想将指南中的 Distroless 映像 (gcr.io/distroless/base-debian10) 中的第二层映像切换为 alpine:3。当我进行更改、构建新映像并尝试使用上面的相同命令运行新映像时,我收到有关用户不存在的错误:

docker: Error response from daemon: unable to find user nonroot: no matching entries in passwd file.

这对我来说很有意义。听起来 Distroless 映像有该用户,而 Alpine 映像没有。所以我从 Dockerfile 中删除了 USER nonroot:nonroot 。因此,此时,我的 Dockerfile 的后半部分如下所示:

##
## Deploy
##

FROM alpine:3

WORKDIR /

COPY --from=build /docker-gs-ping /docker-gs-ping

EXPOSE 8080

ENTRYPOINT ["/docker-gs-ping"]

然后我构建了一个新映像,并尝试使用上面的相同命令来运行新映像。这次,我收到以下错误:

standard_init_linux.go:228: exec user process caused: no such file or directory

我无法解决此错误。为什么以这种方式切换我的基础映像会导致此错误?

I'm following the guide at https://docs.docker.com/language/golang/build-images/ to learn the best way to do a multistage Docker build for a Go application. I cloned the repo:

git clone https://github.com/olliefr/docker-gs-ping

And I ran the command near the bottom of the guide to build the image:

docker build -t docker-gs-ping:multistage -f Dockerfile.multistage .

I also ran my own command to run a temporary container from the image:

docker run --rm docker-gs-ping:multistage

This worked fine. I see the application's output in my terminal. However, I wanted to switch the second layer image from the Distroless image in the guide (gcr.io/distroless/base-debian10) to alpine:3. When I made that change, built a new image, and tried to run the new image using the same command from above, I got an error about the user not existing:

docker: Error response from daemon: unable to find user nonroot: no matching entries in passwd file.

That made sense to me. It sounds like the Distroless image has that user and the Alpine image doesn't. So I removed USER nonroot:nonroot from the Dockerfile. So at this point, the second half of my Dockerfile looks like this:

##
## Deploy
##

FROM alpine:3

WORKDIR /

COPY --from=build /docker-gs-ping /docker-gs-ping

EXPOSE 8080

ENTRYPOINT ["/docker-gs-ping"]

Then I built a new image and tried to run the new image using the same command from above. This time, I got the following error:

standard_init_linux.go:228: exec user process caused: no such file or directory

I'm having trouble troubleshooting this error. Why does switching my base image this way cause this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

寄与心 2025-01-17 01:36:33

我的基本图像不同步。用于构建二进制文件的阶段中使用的基础映像也需要是 Alpine。如果我将指南中该 Dockerfile 的第一行从 FROM golang:1.16 AS build 切换到 FROM golang:1.16-alpine AS build,它就可以工作。

My base images aren't in sync. The base image used in the stage used to build the binary needs to be Alpine too. If I switch the first line of that Dockerfile in the guide from FROM golang:1.16 AS build to FROM golang:1.16-alpine AS build, it works.

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