Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 890
Alexander Mills Avatar asked Apr 16 '26 06:04

Alexander Mills


2 Answers

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
like image 95
Alexander Mills Avatar answered Apr 19 '26 09:04

Alexander Mills


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.

like image 38
SaW Avatar answered Apr 19 '26 08:04

SaW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!