英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论