Docker scratch image for golang app cant find binary "no such file or directory"

  docker, go

I have created a dockerfile that builds a scratch image from which it serves a static golang build. When I start a container from the image, I receive this error:

idea_service_prod | standard_init_linux.go:211: exec user process caused "no such file or directory"
idea_service_prod exited with code 1

The dockerfile:

FROM golang:1.14 as base

# install librdkafka
ENV LIBRDKAFKA_VERSION=1.4.2
RUN curl -Lk -o /root/librdkafka-${LIBRDKAFKA_VERSION}.tar.gz https://github.com/edenhill/librdkafka/archive/v${LIBRDKAFKA_VERSION}.tar.gz && 
    tar -xzf /root/librdkafka-${LIBRDKAFKA_VERSION}.tar.gz -C /root && 
    cd /root/librdkafka-${LIBRDKAFKA_VERSION} && 
    ./configure --prefix /usr && make && make install && make clean && ./configure --clean

# install inotify-tools
FROM base as dev
EXPOSE 80
RUN apt-get -y update 
    && apt-get install -y --no-install-recommends inotify-tools 
    && apt-get clean

FROM base as build

# install git
RUN apt-get -y update 
    && apt-get install -y --no-install-recommends git 
    && apt-get clean

# configure user
ENV USER=appuser
ENV UID=10001
RUN adduser 
    --disabled-password 
    --gecos "" 
    --home "/nonexistent" 
    --shell "/sbin/nologin" 
    --no-create-home 
    --uid "${UID}" 
    "${USER}"

# download dependencies
WORKDIR $GOPATH/src/ideaservice
COPY . .
RUN go get -d -v
RUN go mod download
RUN go mod verify

# build binary
RUN GOOS=linux GOARCH=amd64 go build -tags static -ldflags="-w -s" -o /go/bin/main cmd/ideaservice/main.go

FROM scratch
EXPOSE 80
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/group/ /etc/group
COPY --from=build /go/bin/main /go/bin/main
USER appuser:appuser
CMD ["/go/bin/main"]

What could be causing this? It seems that it should work – the binary is copied from the past stage in the build process, to a destination in the scratch image, where it is then executed from.

Source: StackOverflow

LEAVE A COMMENT