duanpengya7074
2019-05-16 12:25
浏览 193

如何运行使用go二进制文件创建的Docker容器?

I am trying to create a docker container with a Dockerfile and a go file binary.

I have two files in my folder: Dockerfile and main, where the latter is a binary of my simple go file.

Contents of Dockerfile:

FROM golang:1.11-alpine
WORKDIR /app
COPY main /app/
RUN ["chmod", "+x", "/app/main"]
ENTRYPOINT ["./main"]

I tried following steps:

  1. sudo docker build -t naive5cr .
  2. sudo docker run -d -p 8080:8080 naive5cr

The error which i see in thru "docker logs " :

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

my go file content [i think it is irrelevant to the problem]:

func main() {
    http.HandleFunc("/", index)

    http.ListenAndServe(port(), nil)
}

func port() string {
    port := os.Getenv("PORT")
    if len(port) == 0 {
        port = "8080"
    }
    return ":" + port
 } 

the binary "main" runs as expected when run standalone. so there is no problem with the content of go file.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试创建带有Dockerfile和go文件二进制文件的Docker容器。 \ n

我的文件夹中有两个文件: Dockerfile main ,其中后者是我简单的go文件的二进制文件。

Dockerfile的内容

  FROM golang:1.11-alpine 
WORKDIR / app 
COPY main / app / 
RUN [“ chmod”,“ + x”,“  / app / main“] 
ENTRYPOINT [” ./main“] 
   
 
 

我尝试了以下步骤:

  1. sudo docker build -t naive5cr。
  2. sudo docker run -d -p 8080:8080 naive5cr

    错误 我在“ docker日志”中看到了:

    standard_init_linux.go:207:exec用户进程导致“没有此类文件或目录”

    我去了 文件内容[我认为与问题无关]:

      func main(){
     http.HandleFunc(“ /”,index)
     
     http。  ListenAndServe(port(),nil)
    } 
     
    func port()字符串{
     port:= os.Getenv(“ PORT”)
    如果len(port)== 0 {
      port =“ 8080” 
    } 
    返回“:” + port 
    } 
       
     
     

    二进制文件“ main”在独立运行时会按预期运行。 因此go文件的内容没有问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyo770657 2019-05-16 12:44
    已采纳

    You need to compile with CGO_ENABLED=0 to prevent links to libc on Linux when networking is used in Go. Alpine ships with musl rather than libc, and attempts to find libc result in the no such file or directory error. You can verify this by running ldd main to see the dynamic links.

    You can also build on an Alpine based host to link to musl instead of libc. The advantage of a completely statically compiled binary is the ability to run on scratch, without any libraries at all.

    点赞 打赏 评论
  • doujiekeyan0622 2019-05-16 12:31

    go compiles down to native code, so make sure to build your go code on the Docker image, instead of copying the binary to the docker image.

    e.g.

    FROM golang:1.11-alpine
    WORKDIR /app
    ADD . /app
    RUN cd /app && go build -o goapp
    ENTRYPOINT ./goapp
    

    Also as a bonus, here is how to create really tiny Docker images with multistage Docker builds:

    FROM golang:1.11-alpine AS build-env
    ADD . /src
    RUN cd /src && go build -o goapp
    
    FROM alpine
    WORKDIR /app
    COPY --from=build-env /src/goapp /app/
    ENTRYPOINT ./goapp
    
    点赞 打赏 评论

相关推荐 更多相似问题