Home > database >  docker run "exec format error" on macOS m1 and linux x86
docker run "exec format error" on macOS m1 and linux x86

Time:08-17

I'm trying to build and run a fairly simple Go application in Docker. Local build and dev environment is a Mac with m1 processor, and target running environment is AWS ECS Linux x86_64.

Docker build commands run clean, however when trying to run the container, I am unable to get past exec format error regardless of various go build and docker build generic and platform specific options.

Dockerfile:

# syntax=docker/dockerfile:1

## Build
FROM golang:1.16-buster AS build

LABEL Author="Adam Gantt ([email protected])"

ENV APPPATH=api/src
WORKDIR /app


COPY ${APPPATH}/go.mod ./
COPY ${APPPATH}/go.sum ./
COPY ${APPPATH}/*.go ./

RUN go mod download
RUN go get .

# Generic build
RUN go build -o /docker-api_gateway

# Build for Mac m1
# RUN GOOS=darwin GOARCH=arm64 go build -o /docker-api_gateway
# Build for Windows
# RUN GOOS=windows GOARCH=amd64 go build -o /docker-api_gateway

# Add the required env file to the distro
COPY ${APPPATH}/.env /docker-api_gateway

RUN chmod 777 /docker-api_gateway

## Deploy
FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /docker-api_gateway /docker-api_gateway

EXPOSE 9001

USER nonroot:nonroot

ENTRYPOINT ["/docker-api_gateway"]

Notice I have a couple of platform specific RUN go build commands that I have been testing with no success

I prefer to build the image using the following command:

$ docker build -t crm-plus-api-gateway:latest -f ./api/Dockerfile .

And on my Mac, I have used the following command:

$ docker build --platform linux/arm64/v8 -t crm-plus-api-gateway:latest -f ./api/Dockerfile .

I also have a Windows laptop for testing and have platform specific builds, but I continue to get the exec format error regardless of which toggles I switch in the Dockerfile or the docker build command.

I've spent more time than I care to admit on this site and Google trying to find troubleshooting tips, so reaching out for help on additional suggestions to get this container running.

The GO app is a single module with no private modules, and runs successfully on my local machines.

File structure of the GO app:

root
  |> api
     |> src
        |.env
        |go.mod
        |go.sum
        |main.go

I'd like to get a successful container running on my local environments (Mac & Windows) before I move forward with pushing the image to AWS.

CodePudding user response:

This is overwriting your binary docker-api_gateway with the .env file

COPY ${APPPATH}/.env /docker-api_gateway

  • Related