standard_init_linux.go:190: exec user process caused “no such file or directory” Docker with go basic web app

前端 未结 5 2007
野性不改
野性不改 2020-12-21 07:32

The very basic web app is created in Go

package main

import(
   "fmt"
   "net/http"
   "os"
)

func hostHandler(w http.Response         


        
相关标签:
5条回答
  • 2020-12-21 07:44

    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"]
    
    0 讨论(0)
  • 2020-12-21 07:53

    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

    0 讨论(0)
  • 2020-12-21 08:01

    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

    0 讨论(0)
  • 2020-12-21 08:04

    Try change line in your Dockerfile

    COPY webapp /webapp

    0 讨论(0)
  • 2020-12-21 08:06

    I used this and it works

    env GOARCH=386 GOOS=linux go build webapp.go
    
    0 讨论(0)
提交回复
热议问题