The very basic web app is created in Go
package main
import(
"fmt"
"net/http"
"os"
)
func hostHandler(w http.Response
In my case CGO_ENABLED=0
was the key to fix this problem.
Cgo allows to use inline C code in Go sources, see more: https://golang.org/cmd/cgo/
I reckon by default Cgo links your application dynamically to libc, even if you don't use any inline C.
And libc is missing when you package your application to Docker image FROM scratch
Here is my working Dockerfile:
FROM golang:1.9.2-alpine AS builder
WORKDIR /go/src/app
COPY . .
RUN CGO_ENABLED=0 go install
FROM scratch
WORKDIR /opt
COPY --from=builder /go/bin/app .
ENTRYPOINT ["/opt/app"]
File not found can mean the file is missing, a script missing the interpreter, or an executable missing a library. In this case, the net import brings in libc by default, as a dynamic linked binary. You should be able to see that with ldd
on your binary.
To fix it, you'll need to pass some extra flags:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w' -o mybin *.go
The above is from: https://medium.com/@diogok/on-golang-static-binaries-cross-compiling-and-plugins-1aed33499671
In my case en env was not enough: RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64
I had to add as well in the build the tags and ldflags: RUN go build -a -tags netgo -ldflags '-w' -o /go/bin/myService *.go
Try change line in your Dockerfile
COPY webapp /webapp
I used this and it works
env GOARCH=386 GOOS=linux go build webapp.go