2019-05-16 12:25
浏览 288


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
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文件的二进制文件。


  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日志”中看到了:


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

      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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题