无法在 Intel Mac 上的 Docker 中运行 HelloWorld Go 程序

huangapple go评论80阅读模式
英文:

Can't run a HelloWorld Go program in Docker on Intel Mac

问题

我在尝试在 Intel Mac 上运行一个 helloWorld 程序时遇到了 standard_init_linux.go:228: exec user process caused: exec format error 错误。在 Docker 外部运行正常。

hello.go

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

Dockerfile

FROM ubuntu
VOLUME ["/data"]
WORKDIR /data
ENTRYPOINT ["/data/hello"]
$ go build ./hello.go 
$ docker build -t aaa . 
$ docker run -v ${PWD}:/data  aaa

standard_init_linux.go:228: exec user process caused: exec format error

$ uname -a                                                                                                      
Darwin ###-MBP 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; #####/RELEASE_X86_64 x86_64
英文:

I am getting a standard_init_linux.go:228: exec user process caused: exec format error while trying to run a helloWorld on Intel Mac. Runs fine outside of docker.

hello.go

package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

Dockerfile

FROM ubuntu
VOLUME ["/data"]
WORKDIR /data
ENTRYPOINT ["/data/hello"]
$ go build ./hello.go 
$ docker build -t aaa . 
$ docker run -v ${PWD}:/data  aaa

standard_init_linux.go:228: exec user process caused: exec format error

$ uname -a                                                                                                      
Darwin ###-MBP 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; #####/RELEASE_X86_64 x86_64

答案1

得分: 5

你正在为 macOS 构建一个二进制文件,并尝试在 Linux 容器上运行它。

有两件事可以做来解决这个问题:

  • 在 Dockerfile 中添加步骤,在容器内部编译你的代码
  • 在本地的 Mac 上进行编译时,通过设置环境变量 GOOS=linux 进行交叉编译,将其编译为 Linux 版本。
英文:

You are building a binary for macOS and attempting to run it on a Linux container.

There are two things you can do to fix this:

  • add steps to the Dockerfile to compile your code in the container itself
  • when compiling on the Mac locally, cross compile it for Linux by setting the environment variable GOOS=linux

huangapple
  • 本文由 发表于 2022年5月10日 14:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/72181417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定