强制 docker 在 macOS 上默认使用 linux/amd64 平台

发布于 2025-01-16 12:55:46 字数 473 浏览 3 评论 0原文

当前 beta 版本的 docker 要求您每次需要构建运行 amd64 映像时指定--platform=linux/amd64容器。

文档提到

运行具有多架构支持的映像时,docker 将自动选择与您的操作系统和架构相匹配的映像变体。

该文档没有指定使用环境变量改变这种自动行为的方法。它似乎忽略了 BUILDPLATFORM 和 TARGETPLATFORM。

有没有其他方法可以强制 docker 在平台 linux/amd64 上运行所有 buildrun 命令在 Apple-Silicon 上运行的 macOS 上默认使用 linux/arm64/v8 吗?

Current beta version of docker requires you to specify a --platform=linux/amd64 each time you need to build or run an amd64 image/container.

The documentation mentions

When running an image with multi-architecture support, docker will automatically select an image variant which matches your OS and architecture.

The documentation does not specify a way to alter this automatic behaviour using env variables. It seems to ignore both BUILDPLATFORM and TARGETPLATFORM.

Is there any other way to force docker to run all build and run commands with a platform linux/amd64 instead of linux/arm64/v8 by default on macOS running on apple-silicon?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只为守护你 2025-01-23 12:55:48

您可以在 docker-compose.yaml 中添加:

services:
  service_name:
    environment:
      - DOCKER_DEFAULT_PLATFORM=linux/amd64

You can add in your docker-compose.yaml:

services:
  service_name:
    environment:
      - DOCKER_DEFAULT_PLATFORM=linux/amd64
守护在此方 2025-01-23 12:55:48

将这段代码添加到 ~/.zshrc 和 ~/.bashrc 中。它允许您在执行 docker run 命令时不重复该标志:

# useful only for Mac OS Silicon M1, 
# still working but useless for the other platforms
docker() {
  if [[ `uname -m` == "arm64" ]] && [[ "$1" == "run" || "$1" == "build" ]]; then
     /usr/local/bin/docker "$1" --platform linux/amd64 "${@:2}"
  else
     /usr/local/bin/docker "$@"
  fi
}

Add this snipped to your ~/.zshrc and ~/.bashrc. It allows you not to repeat the flag anytime you perform a docker run command:

# useful only for Mac OS Silicon M1, 
# still working but useless for the other platforms
docker() {
  if [[ `uname -m` == "arm64" ]] && [[ "$1" == "run" || "$1" == "build" ]]; then
     /usr/local/bin/docker "$1" --platform linux/amd64 "${@:2}"
  else
     /usr/local/bin/docker "$@"
  fi
}
尹雨沫 2025-01-23 12:55:47

您可以设置环境变量DOCKER_DEFAULT_PLATFORM

export DOCKER_DEFAULT_PLATFORM=linux/amd64

You can set the environment variable DOCKER_DEFAULT_PLATFORM:

export DOCKER_DEFAULT_PLATFORM=linux/amd64
就像说晚安 2025-01-23 12:55:47

使用 Apple Silicon(或其他基于 ARM64 的架构)构建的 Docker 映像在将映像部署到 Linux 或 Windows 时可能会出现问题/em> 基于 *AMD64 环境(例如 AWS EC2ECS 等)。例如,您可能尝试将在 M1 芯片上制作的 docker 映像上传到 AWS ECR 存储库,但它无法运行。因此,您需要一种在 ARM64 架构上构建基于 AMD64 的映像的方法,无论是使用 Docker 构建(针对单个映像)还是 docker-compose 构建(例如,对于在docker compose网络中运行的多图像应用程序)。

用于构建单个 docker 镜像:
使用命令行设置环境变量或按照接受的答案中的建议修改 .bashrc.zshenv 文件。

export DOCKER_DEFAULT_PLATFORM=linux/amd64

或者,在 Dockerfile 中,在 FROM 命令中包含以下标志(对于多阶段 Dockerfile 构建,仅需要该标志第一阶段):

FROM --platform=linux/amd64 python:3.7-alpine

要将镜像构建为 docker-compose 构建的一部分,请为每个服务包含 platform: linux/amd64。例如:

  services:  
    frontend:  
      platform: linux/amd64
      build: frontend  
      ports:
        - 80:80  
      depends_on:
        - backend  
    backend:  
      platform: linux/amd64
      build: backend  

Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows based *AMD64 environment (e.g. AWS EC2, ECS, etc.). For example, you may try to upload your docker image made on the M1 chip to an AWS ECR repository and it fails to run. Therefore, you need a way to build AMD64 based images on the ARM64 architecture, whether it's using Docker build (for individual images) or docker-compose build (e.g. for multi-image apps running in a docker compose network).

For building single docker images:
Set your environment variable using the command line or modifying your .bashrc or .zshenv file as suggested in the accepted answer.

export DOCKER_DEFAULT_PLATFORM=linux/amd64

Alternatively, in the Dockerfile, include the following flag in the FROM command (for a multi-stage Dockerfile build, the flag is only needed for the first stage):

FROM --platform=linux/amd64 python:3.7-alpine

For building images as part of a docker-compose build, include the platform: linux/amd64 for each service. For example:

  services:  
    frontend:  
      platform: linux/amd64
      build: frontend  
      ports:
        - 80:80  
      depends_on:
        - backend  
    backend:  
      platform: linux/amd64
      build: backend  
另类 2025-01-23 12:55:47

您不需要像答案之一中提到的那样导出 env 变量,您可以通过执行以下操作将其作为命令的一部分运行:

DOCKER_DEFAULT_PLATFORM=linux/amd64 docker-compose build

请记住,如果您已经下载了不同平台的映像,无论您将哪个平台指定为默认平台,docker 都会继续使用该映像,您需要首先使用 docker image rm your_img 删除该映像来修复该问题。

You don't need to export the env variable as mentioned in one of the answers, you can run it as part of the command a single time by doing:

DOCKER_DEFAULT_PLATFORM=linux/amd64 docker-compose build

Keep in mind that if you've already downloaded the image for a different platform, docker will keep using that image no matter what platform you specify as your default, you would delete the image using docker image rm your_img first to fix that.

独﹏钓一江月 2025-01-23 12:55:47

您可以使用支持 cli 平台的 buildx (mobi)。

docker buildx build --platform linux/amd64 .

You can use buildx (mobi) which suipport cli for platform.

docker buildx build --platform linux/amd64 .
少钕鈤記 2025-01-23 12:55:47

您可以

export DOCKER_DEFAULT_PLATFORM=linux/amd64

在 Mac M1 的 .zshrc 文件中进行设置

you can set

export DOCKER_DEFAULT_PLATFORM=linux/amd64

in a .zshrc file for Mac M1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文