"exec format error" 当 运行 容器使用 Apple M1 芯片构建时(基于 ARM 的系统)

"exec format error" when running containers build with Apple M1 Chip (ARM based systems)

预期行为:我可以 运行 我使用 Apple M1 芯片构建的容器。

观察到的行为:

假设您有一个 Google Cloud 运行 帐户并且可以将 Docker 图像推送到 Google Container Registry。我在这个例子中使用 https://github.com/seenickcode/trivial-go-api

  1. `git 克隆 git@github.com:seenickcode/trivial-go-api.git'
  2. cd trivial-go-api
  3. docker build -t gcr.io/<YOUR GCR PROJECT ID>/example .
  4. docker push -t gcr.io/<YOUR GCR PROJECT ID>/example
  5. 转到 console.cloud.google.com、Google 云 运行 > 创建新服务 > select 你推送的 Docker 图像所有默认选项 > 运行
  6. 显示错误:
Cloud Run error: Container failed to start. 
Failed to start and then listen on the port defined by the PORT environment variable. 
Logs for this revision might contain more information.

日志:

2021-04-02 09:35:40.045 EDT
Cloud Run ReplaceService example hello@redactedforso.com {@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, authorizationInfo: […], methodName: google.cloud.run.v1.Services.ReplaceService, request: {…}, requestMetadata: {…}, resourceLocation: {…}, resourceName: namespaces/myprojectforso-282419/services/example, response: {…}, servi…
Error
2021-04-02 09:35:49.034 EDT
terminated: Application failed to start: Failed to create init process: failed to load /app/main: exec format error
Warning
2021-04-02 09:35:49.174 EDT
Application exec likely failed
Notice
2021-04-02 09:57:43.102 EDT
Cloud Run ReplaceService example hello@redactedforso.com {@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, authorizationInfo: […], methodName: google.cloud.run.v1.Services.ReplaceService, request: {…}, requestMetadata: {…}, resourceLocation: {…}, resourceName: namespaces/myprojectforso-282419/services/example, response: {…}, servi…
Error
2021-04-02 09:57:50.657 EDT
terminated: Application failed to start: Failed to create init process: failed to load /app/main: exec format error

有关我在哪里构建图像的系统详细信息:

重要提示:

您正在构建 Google 云不支持的 ARM 兼容映像。

我在将我的 Mac M1 构建映像推送到 Heroku 时遇到了类似的问题,我使用 buildx 并设置了预期的平台

解决了这个问题
docker buildx build --platform linux/amd64 -t myapp .

我写了一个Mediumpost来说明问题,提出2个解决方案。

Docker is by design multi-platform and can run on different architectures, however, the images must match the platform they will be run on. Which is not our case.