hy c
hy c

Reputation: 115

build small image from scratch (open: no such file or directory)

I tried to use scratch to build a small image. I turned off the CGO but still fail to read the file when the program runs. I have got the error: "open ./app/a.txt: no such file or directory". Is there other reasons that the program cannot read the file?

FROM golang:alpine AS builder

RUN apk update && apk add --no-cache git

WORKDIR $GOPATH/src/scratch
ADD . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o /go/bin/scratch

FROM scratch
# Copy static executable.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/bin/scratch /go/bin/scratch
# Run the binary.
ENTRYPOINT ["/go/bin/scratch"]
func main() {
    resp, err := http.Get("https://google.com")
    check(err)
    body, err := ioutil.ReadAll(resp.Body)
    check(err)
    fmt.Println(len(body))
    LocalFile := "./app/a.txt"
    fmt.Println(LocalFile)

    dat, err := ioutil.ReadFile(LocalFile)
    check(err)
    fmt.Print(string(dat))

    f, err := os.Open(LocalFile)
    check(err)

    b1 := make([]byte, 5)
    n1, err := f.Read(b1)
    check(err)
    fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))
}
func check(err error) {
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

Upvotes: 2

Views: 1436

Answers (2)

hy c
hy c

Reputation: 115

I made a mistake to compile the go program with the txt file the program would like to read. That's why the file path was not correct once the program compiled to binary exec. Just in case someone made the same mistake like me, here's is one of my solution. I solved the problem by moving the text file outside of my project and use env variable to point to the path i want.

I think package "osext" is another way to do it. Find the path to the executable

Upvotes: 0

frozenOne
frozenOne

Reputation: 554

alpine does not provide glibc. alpine is that small because it uses a stripped down version of libstdc called musl.libc.org.

So we'll check statically linked dependencies using ldd command.

$ docker run -it <image name> /bin/sh
$ cd /go/bin
$ ldd scratch   # or the excutable you are calling-> ldd <executable>

Check the linked static files, do they exist on that version of alpine? If the do not from, the binaries perspective, it did not find the file-- and will report File not found.

The following step depends on what binaries were missing, you can look it up on the internet on how to install them.

Add RUN apk add --no-cache libc6-compat to your Dockerfile to add libstdc in some Golang alpine image based Dockerfiles.

In you case the solution is to either

  • disable CGO : use CGO_ENABLED=0 while building
  • or add RUN apk add --no-cache libc6-compat to your Dockerfile
  • or do not use golang:alpine

Upvotes: 2

Related Questions