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

pn9klfpd  于 2022-12-03  发布在  Docker
关注(0)|答案(3)|浏览(200)

Expected behavior: I can run a container I've built using an Apple M1 chip.
Observed behavior:
Assuming you have a Google Cloud Run account and can push Docker images to Google Container Registry. I'm using https://github.com/seenickcode/trivial-go-api for this example.

  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. 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
  • Chip: Apple M1
  • Docker version: Docker Desktop for macOS v3.3.0 (62345)

Important Notes:

  • This all works completely fine for me when I use another architecture, i.e. via Google Container Build or my home Windows (WSL) desktop.
  • 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. 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.
n53p2ov0

n53p2ov01#

您正在构建Google Cloud不支持的ARM兼容映像。
我遇到了一个类似的问题,将我的Mac M1构建的映像推到Heroku,我使用buildx和设置预期的平台解决了这个问题

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

我写了一篇Medium文章来解释这个问题,并提出了2种解决方案。
Docker是多平台设计,可以在不同的架构上运行,但是,映像必须与它们将运行的平台相匹配。

46scxncf

46scxncf2#

从Docker API版本1.40开始,您可以使用--platform选项指定需要在其上构建映像的平台,而无需使用buildx。

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

kkbh8khc3#

您还可以构建x86兼容的映像,并使用单个命令将其推送到工件注册表:

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

相关问题