在 macOS M1 和 Linux x86 上运行时出现 “exec format error” 的错误。

huangapple go评论83阅读模式
英文:

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

问题

我正在尝试在Docker中构建和运行一个相当简单的Go应用程序。本地构建和开发环境是搭载M1处理器的Mac,目标运行环境是AWS ECS Linux x86_64。

Docker构建命令运行正常,但是当尝试运行容器时,无论使用哪种go build和docker build通用和特定平台的选项,都无法解决"exec format error"的问题。

Dockerfile:

# syntax=docker/dockerfile:1

## Build
FROM golang:1.16-buster AS build

LABEL Author="Adam Gantt (adam@adamgantt.com)"

ENV APPPATH=api/src
WORKDIR /app


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

RUN go mod download
RUN go get .

# 通用构建
RUN go build -o /docker-api_gateway

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

# 将所需的env文件添加到发行版
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"]

注意,我有几个特定平台的RUN go build命令,但是我一直在测试中没有成功

我更喜欢使用以下命令构建镜像:

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

在我的Mac上,我使用了以下命令:

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

我还有一台用于测试的Windows笔记本电脑,并且有特定平台的构建,但是无论我在Dockerfile或docker build命令中切换哪些开关,我仍然遇到"exec format error"的问题。

我在这个网站和Google上花了比我愿意承认的更多时间,试图找到故障排除的提示,所以现在寻求帮助,看看是否有其他建议可以让这个容器运行起来。

这个Go应用程序是一个单一模块,没有私有模块,在我的本地机器上成功运行。

GO应用程序的文件结构:

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

在将镜像推送到AWS之前,我希望在我的本地环境(Mac和Windows)上成功运行一个容器。

英文:

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 (adam@adamgantt.com)"

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.

答案1

得分: 1

这将使用.env文件覆盖你的二进制文件docker-api_gateway

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

英文:

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

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

huangapple
  • 本文由 发表于 2022年8月16日 19:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73373068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定