Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"no such file or directory" with docker scratch image

Tags:

docker

go

Getting an error:

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

this error disappear when using alpine image.

this is my Dockerfile:

ARG PROJ_NS=gitlab.com/jonas.jasas
ARG PROJ_NAME=httprelay
ARG PROJ_BIN_PATH=/$PROJ_NAME

################################################################################
FROM golang:alpine
RUN apk update && apk add --no-cache git

ARG PROJ_NS
ARG PROJ_NAME
ARG PROJ_BIN_PATH

RUN go get -d $PROJ_NS/$PROJ_NAME/...
WORKDIR $GOPATH/src/$PROJ_NS/$PROJ_NAME
RUN go get -d ./cmd/...
RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o $PROJ_BIN_PATH ./cmd/...

RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_passwd

################################################################################
#FROM alpine
FROM scratch

ARG PROJ_BIN_PATH

COPY --from=0 /etc_passwd /etc/passwd
COPY --from=0 $PROJ_BIN_PATH /entrypoint

USER nobody

EXPOSE 8800
ENTRYPOINT ["/entrypoint"]
like image 996
Jonas Avatar asked Mar 11 '19 16:03

Jonas


1 Answers

Turn off CGO since that can result in dynamic links to libc/libmusl.

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
    -ldflags="-w -s" -o $PROJ_BIN_PATH ./cmd/...

The no such file or directory error with a binary indicates either you did not call your binary by the right name, or that your binary is dynamically linked to a library that does not exist.

You can check for dynamic links with ldd, e.g.:

docker build --target=0 -t your_go_image .
docker run -it --rm your_go_image ldd /$PROJ_NAME

Here's an example with one of my own projects:

$ go build -o test .

$ ldd test
        linux-vdso.so.1 (0x00007ffd2ebd8000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2dda9ed000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2dda64e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2ddac0a000)

$ CGO_ENABLED=0 go build -o test .                                                                                                     

$ ldd test
        not a dynamic executable
like image 163
BMitch Avatar answered Sep 30 '22 15:09

BMitch