linux - Golang + Linux + Docker 错误 : standard_init_linux. go:211:没有那个文件或目录

标签 linux docker go

这不是重复的,因为虽然错误相同,但我的用例与其他用例不同。
我正在编译一个使用 docker 部署的 go 应用程序:

  • 最新的 Arch Linux
  • 最新的 docker 版本,golang:alpine 镜像
  • 尝试使用 go 版本 1.13.3 和 1.14.4 linux amd64
  • 我没有 bash 脚本或错误的文件结尾。整个项目都写在这台 Linux 机器上
  • 我可以部署一个空的 go 应用程序,它只有一个 fmt 打印,没有任何错误

  • 然而,
    当我在我的 OSX 机器上构建它并将其发送到 linux 时,我可以将该可执行文件部署到 docker 而不出现任何错误
  • OSX mojave
  • 最新的 docker
  • 去 1.13.3
  • GOOS=linux

  • 错误 :standard_init_linux.go:211: exec user process caused "no such file or directory"

    最佳答案

    当您使用 cgo 编译您的 go 应用程序时启用编译器动态链接到 libstdc .
    然而,golang:alpine图像如此之小,因为它没有使用 libstdc但它的简化版本称为 musl libc .
    错误消息说

    standard_init_linux.go:211: exec user process caused "no such file or directory"
    
    如果你连接到你的图像
    $ docker run -it [image] /bin/sh
    
    您可以在比方说 /dist/myexec 中找到您的可执行文件,但是当您尝试运行该可执行文件时,它会显示 err not found ,显然不是因为它找不到您的可执行文件,而是找不到文件 libc .
    解决方案是要么
  • 禁用 CGO:使用 CGO_ENABLED=0在 build 时
  • 或添加
    RUN apk add --no-cache libc6-compat
    
    给您的 Dockerfile
  • 或不使用 golang:alpine

  • 要拥有一个全静态二进制可执行文件,请使用以下命令构建它:
    $ CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .
    

    关于linux - Golang + Linux + Docker 错误 : standard_init_linux. go:211:没有那个文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62632340/

    相关文章:

    objective-c - objective c linux 编译脚本

    postgresql - 官方 Postgres 镜像如何使用环境变量创建用户和数据库?

    docker - 找不到docker secrets的文件夹('/run/secrets')

    go - 如何从接口(interface)中提取具体结构

    file - Golang 中的 bufio.NewScanner 是否读取内存中的整个文件而不是每个文件一行?

    linux - cp : target '/root/var/www/html/' is not a directory

    linux - 有没有办法在 bash 中执行 Linux ifup 或 ifdown 而不必总是先输入 sudo?

    go - golang 中 GPU 上的向量加法

    linux - 使用 "read"设置变量

    docker - Kubernetes部署数据库连接错误