简体   繁体   中英

"Exec format error" with docker run command

I have this Golang based 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. I get this error:

/bin/sh: 1: ./huru: Exec format error

anyone know what that is about? "docker build" succeeds, but "docker run" fails with that error.

If you want to run the docker image on Macos then just specifying the target OS is sufficient:

Assuming there is a src and bin folder, execute in the src folder:

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

(this works with m1, uses the arm64 architecture)

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.

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. That don't work.

Here is the Dockerfile that worked for me:

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:

$GOPATH/
      src/
        huru/
      .dockerignore
      Dockerfile

I run:

docker build -t foo .
docker run foo

my .dockerignore file contains:

.vscode
bin
pkg

You could build your application ( huru ) for the target architecture in MacOS and then copy it into the docker image. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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