简体   繁体   English

“执行格式错误”与 docker 运行命令

[英]"Exec format error" with docker run command

I have this Golang based Dockerfile:我有这个基于 Golang 的 Dockerfile:

FROM golang:latest

RUN mkdir -p /app

WORKDIR /app

COPY bin/huru .

CMD ./huru

I checked and the huru binary file is in the working dir.我检查了一下,huru 二进制文件在工作目录中。 I get this error:我收到此错误:

/bin/sh: 1: ./huru: Exec format error /bin/sh: 1: ./huru: 执行格式错误

anyone know what that is about?有人知道那是什么吗? "docker build" succeeds, but "docker run" fails with that error. “docker build”成功,但“docker run”失败并出现该错误。

If you want to run the docker image on Macos then just specifying the target OS is sufficient:如果您想在 Mac 上运行 docker 图像,那么只需指定目标操作系统就足够了:

Assuming there is a src and bin folder, execute in the src folder:假设有src和bin文件夹,在src文件夹下执行:

env GOOS=linux go build -o../bin环境 GOOS=linux go build -o../bin

(this works with m1, uses the arm64 architecture) (这适用于 m1,使用 arm64 架构)

BTW I would not use latest, I see that there is a docker image based on 1.20 which is not yet officially released at time of writing.顺便说一句,我不会使用最新的,我看到有一个基于 1.20 的 docker 图像,在撰写本文时尚未正式发布。

The "Exec format error" was simply because I was copying the binary file built on OSX/MacOS into the Docker image and trying to run that binary file in the Linux container. “ Exec格式错误”仅是因为我将基于OSX / MacOS构建的二进制文件复制到Docker映像中,并试图在Linux容器中运行该二进制文件。 That don't work. 那不行

Here is the Dockerfile that worked for me: 这是为我工作的Dockerfile:

FROM golang:latest

RUN mkdir -p /app

WORKDIR /app

COPY . .

ENV GOPATH /app

RUN go install huru

ENTRYPOINT /app/bin/huru

and my project structure like so on my host fs: 和我的项目结构在主机fs上是这样的:

$GOPATH/
      src/
        huru/
      .dockerignore
      Dockerfile

I run: 我跑:

docker build -t foo .
docker run foo

my .dockerignore file contains: 我的.dockerignore文件包含:

.vscode
bin
pkg

You could build your application ( huru ) for the target architecture in MacOS and then copy it into the docker image. 您可以在MacOS中为目标体系结构构建应用程序( huru ),然后将其复制到docker映像中。 To build for the target architecture you have to use command in the following format: env GOOS=linux GOARCH=amd64 go build -o application main.go This has the added advantage of having a clean dockerfile and smaller image. 要针对目标体系结构进行构建,您必须使用以下格式的命令: env GOOS=linux GOARCH=amd64 go build -o application main.go这具有具有干净的dockerfile和较小映像的附加优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM