在前期编译的二进制文件在临时容器中不起作用

Binary compiled in prestage doesn't work in scratch container

我有一个多阶段的 Dockerfile,它编译 netcat 以便稍后在临时映像中使用它。 (这个没有什么特别的用处,只是为了测试这个是怎么做到的。)

FROM alpine:latest AS build
RUN wget -q -O /root/netcat.tar.bz2 https://vorboss.dl.sourceforge.net/project/netcat/netcat/0.7.1/netcat-0.7.1.tar.bz2 && \
apk --no-cache add --update gcc g++ make
WORKDIR /root/
RUN tar xvjf /root/netcat.tar.bz2
WORKDIR /root/netcat-0.7.1/
RUN ./configure && make && make install

FROM scratch # When using alpine here it works
WORKDIR /bin/
COPY --from=build /usr/local/bin/netcat /bin/netcat
ENTRYPOINT ["/bin/netcat"]

当运行这张图片时,我得到以下错误:

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

文件在那里是因为它在使用 alpine 作为第二张图片时有效。使用 busybox 时,我可以使用 /bin/sh 作为入口点,但是当从那个 shell 执行 netcat 时,它也不起作用。

我这里有不同架构的问题吗?我不知道发生了什么...

谢谢

这是因为 scratch 基础图像是 明确为空的图像 。它没有共享对象,没有运行时库。

如果要将可执行二进制文件放入 scratch 基本映像,请确保使用 -static 标志编译二进制文件。显然从第一步构建的 netcat 有一些依赖:

$ ldd netcat
linux-vdso.so.1 =>  (0x00007ffe168f1000)
libc.musl-x86_64.so.1 => not found