10

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.

1
  • Thanks for this SO post 🙏 Finally solved the issue Commented Apr 22, 2024 at 16:34

3 Answers 3

12

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
Sign up to request clarification or add additional context in comments.

Comments

10

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.