Search code examples
dockernetcat

Binary compiled in prestage doesn't work in scratch container


I have a multi-stage Dockerfile that compiles netcat to use it in a scratch image later. (This has no particular use, but to test how this is done.)

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"]

When running this image I get the following error:

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

The file is there because it works when using alpine as second image. When using busybox, I could use /bin/sh as entrypoint, but when executing netcat from that shell, it wouldn't work either.

Do I have some problem of different architectures here? I don't know what is happening...

Thanks


Solution

  • It is because the scratch base image is an explicitly empty image. It has no shared object, it has no runtime libraries.

    If you want to put your executable binary into the scratch base image, make sure you compile your binary with -static flags. Apparently the netcat built from the first step has something to depend on:

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