docker - 运行使用 Apple M1 芯片(基于 ARM 的系统)构建的容器时出现“exec 格式错误”
问题描述
预期行为:我可以运行使用 Apple M1 芯片构建的容器。
观察到的行为:
假设您有一个 Google Cloud Run 帐户并且可以将 Docker 映像推送到 Google Container Registry。我在这个例子中使用了https://github.com/seenickcode/trivial-go-api。
- `git clone git@github.com:seenickcode/trivial-go-api.git'
cd trivial-go-api
docker build -t gcr.io/<YOUR GCR PROJECT ID>/example .
docker push -t gcr.io/<YOUR GCR PROJECT ID>/example
- 转到
console.cloud.google.com
,Google Cloud Run > 创建新服务 > 使用所有默认选项选择您推送的 Docker 映像 > 运行 - 显示错误:
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
关于我在哪里构建图像的系统详细信息:
- 操作系统:macOS 11.2.3
- 芯片:苹果M1
- Docker 版本:Docker Desktop for macOS v3.3.0 (62345)
重要笔记:
- 当我使用其他架构(即通过 Google Container Build 或我的家庭 Windows (WSL) 桌面)时,这一切对我来说完全正常。
- 当使用 Apple M1 芯片构建时,这也不适用于其他代码库,例如我用 Rust 和 Dart 编写的另一个项目。似乎与语言无关。
- 我已经使用 Google Cloud Run 多年了,在使用带有 Apple M1 芯片的新笔记本电脑时出现了这个问题。
解决方案
您正在构建 Google Cloud 不支持的与 ARM 兼容的映像。
我在将 Mac M1 构建的映像推送到 Heroku 时遇到了类似的问题,我使用buildx
和设置预期的平台解决了这个问题
docker buildx build --platform linux/amd64 -t myapp .
我写了一篇Medium帖子来解释这个问题并提出 2 个解决方案。
Docker 在设计上是多平台的,可以在不同的架构上运行,但是,镜像必须与它们将运行的平台相匹配。这不是我们的情况。
推荐阅读
- xml - Linearlayout messed up with the horizontal alignment
- python - 如何在决策树中获取特征重要性?
- php - 如何根据 WordPress 中的当前每周日期设置默认类别?
- javascript - access count variable defined in setinterval javascript
- python-3.x - Print output to separate text files
- angular - 为什么我的反应式表单验证在 Angular 中失败
- javascript - 使用 JavaScript 进行媒体查询
- javascript - 如何搜索在 jQuery 数据表中以字符串形式显示的布尔值?
- python - 为什么我的 tf.profiler.Profiler 抱怨“找到加速器操作但错过了加速器流统计信息”
- javascript - 使用浏览器视图在浏览器窗口上设置覆盖