Docker on M1: standard_init_linux.go:219: exec user process caused: exec format error

Viewed 3640

I tried to create a docker image based on alpine, but whenever I try to run it, I get this error message: standard_init_linux.go:219: exec user process caused: exec format error.

Here's the basic Dockerfile that just runs an executable file:

FROM alpine:3.13.5
WORKDIR /usr/local/bin
COPY profiles-svc /usr/local/bin
EXPOSE 20002/tcp
ENTRYPOINT ["/usr/local/bin/profiles-svc"]

The profiles-svc is an executable generated by the go build command.

I did not notice this issue on my ubuntu laptop, happens only on the Macbook M1.

Thanks in advance for the help!

2 Answers

The issue was that there are two different architectures. If you use go build command on M1, it will be arm64, so if you try to execute that on docker with, for example, alpine image, it will fail. To fix that issue, you need to build amd64 based binary. Here's the command: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o out-amd64. Now you can copy that binary to your Linux based dockerfile, build and run.

This is happening because the golang code compiled under Linux arm platform cannot be run under Linux amd platfrom; Similarly, the image built under arm platform will not run on amd platform. The solution is to add the — platform Linux/AMD64 parameter when building your images.

docker build --platform linux/amd64 -t tag .

Related