duanpengya7074 2019-05-16 12:25
浏览 307
已采纳

如何运行使用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.

  • 写回答

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.

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

报告相同问题?

悬赏问题

  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块