为什么将多阶段 Docker 构建中的基础镜像从 distroless 切换到 alpine 会导致“exec user process Caused: no such file or directory”?
我正在遵循 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的基本图像不同步。用于构建二进制文件的阶段中使用的基础映像也需要是 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
toFROM golang:1.16-alpine AS build
, it works.