简体   繁体   English

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

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

Expected behavior: I can run a container I've built using an Apple M1 chip.预期行为:我可以运行使用 Apple M1 芯片构建的容器。

Observed behavior:观察到的行为:

Assuming you have a Google Cloud Run account and can push Docker images to Google Container Registry.假设您有一个 Google Cloud Run 帐户并且可以将 Docker 图像推送到 Google Container Registry。 I'm using https://github.com/seenickcode/trivial-go-api for this example.我在这个例子中使用https://github.com/seenickcode/trivial-go-api

  1. `git clone git@github.com:seenickcode/trivial-go-api.git' `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. Go to console.cloud.google.com , Google Cloud Run > Create new service > select your pushed Docker image with all default options > Run Go to console.cloud.google.com , Google Cloud Run > Create new service > select your pushed Docker image with all default options > Run
  6. Error shown:显示错误:
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.

Logs:日志:

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

System details on where I'm building my image:关于我在哪里构建图像的系统详细信息:

  • OS: macOS 11.2.3操作系统:macOS 11.2.3
  • Chip: Apple M1芯片:苹果M1
  • Docker version: Docker Desktop for macOS v3.3.0 (62345) Docker 版本:Docker Desktop for macOS v3.3.0 (62345)

Important Notes:重要笔记:

  • This all works completely fine for me when I use another architecture, ie via Google Container Build or my home Windows (WSL) desktop.当我使用另一种架构(即通过 Google Container Build 或我的家庭 Windows (WSL) 桌面)时,这一切对我来说完全正常。
  • This also doesn't work with other codebases when built using the Apple M1 Chip, such as another project I have written in Rust as well as Dart.这也不适用于使用 Apple M1 芯片构建的其他代码库,例如我在 Rust 和 Dart 中编写的另一个项目。 Doesn't seem language related.似乎与语言无关。
  • I've been using Google Cloud Run for years, this issue cropped up when using my new laptop with Apple M1 Chip.我已经使用 Google Cloud Run 多年了,在使用带有 Apple M1 芯片的新笔记本电脑时出现了这个问题。

You are building an ARM-compatible image which Google Cloud does not support.您正在构建 Google Cloud 不支持的与 ARM 兼容的映像。

I have hit a similar problem pushing my Mac M1 built image to Heroku, which I solved using buildx and setting the expected platform我遇到了类似的问题,将我的 Mac M1 构建映像推送到 Heroku,我使用buildx解决了这个问题并设置了预期的平台

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

I have written a Medium post to explain the problem and propose 2 solutions.我写了一篇Medium帖子来解释这个问题并提出 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. Docker 在设计上是多平台的,可以在不同的架构上运行,但是,图像必须与它们将运行的平台相匹配。 Which is not our case.这不是我们的情况。

Also you can build x86 compatible image and push to artifact registry with single command:您还可以构建 x86 兼容映像并使用单个命令推送到工件注册表:

docker buildx build \
--platform linux/amd64 \
--push \
-t us-central1-docker.pkg.dev/your_project/your_registry/myapp .

As from docker API version 1.40 , you can specify the platform upon which your image needs to be built by using the --platform option, without using buildx.从 docker API 版本 1.40开始,您可以使用--platform选项指定构建镜像的平台,而无需使用 buildx。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 docker 在 macOS m1 和 linux x86 上运行“exec 格式错误” - docker run "exec format error" on macOS m1 and linux x86 如何使用标准 Apple 版本的 clang 从命令行在 M1 Mac 上构建 Intel 二进制文件? - How to build an Intel binary on an M1 Mac from the command line with the standard Apple version of clang? Apple Silicon Chip 如何在没有 Rosetta 的情况下支持基于 Intel 的 JDK-8? - How does Apple Silicon Chip support Intel based JDK-8 without rosetta? 在 Mac M1 上安装 python 依赖项时出错 - Errors with installing a python dependency on a Mac M1 将映像部署到 K8 - 错误:standard_init_linux.go:228: exec 用户进程导致:exec 格式错误 - Deploying image to K8 - error: standard_init_linux.go:228: exec user process caused: exec format error 云构建错误:“未知生命周期阶段“/root/.m2”” - Cloud Build Error: "Unknown lifecycle phase "/root/.m2"" Flutter ios 在 Macbook air M2 上构建错误 - Flutter ios build error on Macbook air M2 如何在 m1 上安装 x86/64 架构 pod - How to install x86/64 architecture pod on m1 无法在 MacBook M1 中使用 Firebase 运行 Flutter 项目 - Cannot run the Flutter project with Firebase in MacBook M1 在谷歌云构建中的两个容器之间进行通信 - Communicate between two containers in Google cloud build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM