首页 > 解决方案 > 运行使用 Apple M1 芯片(基于 ARM 的系统)构建的容器时出现“exec 格式错误”

问题描述

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

观察到的行为:

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

  1. `git clone 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 Cloud Run > 创建新服务 > 使用所有默认选项选择您推送的 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

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

重要笔记:

标签: dockergoogle-cloud-run

解决方案


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

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

docker buildx build --platform linux/amd64 -t myapp .

我写了一篇Medium帖子来解释这个问题并提出 2 个解决方案。

Docker 在设计上是多平台的,可以在不同的架构上运行,但是,镜像必须与它们将运行的平台相匹配。这不是我们的情况。


推荐阅读