英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论