dsjbest2014 2018-10-04 06:24
浏览 229
已采纳

standard_init_linux.go:190:exec用户进程使用go basic Web应用程序导致“无此文件或目录” Docker

The very basic web app is created in Go

package main

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

func hostHandler(w http.ResponseWriter, r *http.Request){
    name, err :=os.Hostname()

    if err != nil {
           panic(err)
        }

        fmt.Fprintf(w, "<h1>HOSTNAME: %s</h1><br>",name)
        fmt.Fprintf(w, "<h1>ENVIRONMENT VARS: </h1><br>")
        fmt.Fprintf(w, "<ul>")

        for _, evar := range os.Environ(){
            fmt.Fprintf(w, "<li>%s</li>",evar)
        }
        fmt.Fprintf(w, "</ul>")

}

func rootHandler(w http.ResponseWriter, r *http.Request){

    fmt.Fprintf(w, "<h1>Awesome site in Go!</h1><br>")
    fmt.Fprintf(w, "<a href='/host/'>Host info</a><br>")

}

func main() {

        http.HandleFunc("/", rootHandler)
        http.HandleFunc("/host/", hostHandler)
        http.ListenAndServe(":8080", nil)


}

Docker File for it

FROM scratch
WORKDIR /home/ubuntu/go
COPY webapp /
EXPOSE 8080
CMD ["/webapp"]

The image is built successfully

ubuntu@ip-172-31-32-125:~/go/src/hello$ docker build -t "webapp" .
Sending build context to Docker daemon  6.152MB
Step 1/5 : FROM scratch
 --->
Step 2/5 : WORKDIR /home/ubuntu/go
 ---> Using cache
 ---> 8810a06c58c7
Step 3/5 : COPY webapp /
 ---> Using cache
 ---> d75222363d3a
Step 4/5 : EXPOSE 8080
 ---> Using cache
 ---> 45de0853de8e
Step 5/5 : CMD ["/webapp"]
 ---> Using cache
 ---> e9f9031f3632
Successfully built e9f9031f3632
Successfully tagged webapp:latest

But when i run the docker its show error.

ubuntu@ip-172-31-32-125:~/go/src/hello$ docker run webapp
standard_init_linux.go:190: exec user process caused "no such file or directory"

Please guide what is the issue, I am new to docker and go.

Environment-related information

ubuntu@ip-172-31-32-125:~/go/src/hello$ ls
Dockerfile  webapp
ubuntu@ip-172-31-32-125:~/go/src/hello$ echo $GOPATH
/home/ubuntu/go

Code was compiled with go build webapp.go command

  • 写回答

4条回答 默认 最新

  • dongtiran7769 2018-10-04 09:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机