强制 docker 在 macOS 上默认使用 linux/amd64 平台
当前 beta 版本的 docker 要求您每次需要构建
或运行
amd64 映像时指定--platform=linux/amd64
容器。
文档提到
运行具有多架构支持的映像时,docker 将自动选择与您的操作系统和架构相匹配的映像变体。
该文档没有指定使用环境变量改变这种自动行为的方法。它似乎忽略了 BUILDPLATFORM 和 TARGETPLATFORM。
有没有其他方法可以强制 docker
在平台 linux/amd64
上运行所有 build
和 run
命令在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在 docker-compose.yaml 中添加:
You can add in your
docker-compose.yaml
:将这段代码添加到 ~/.zshrc 和 ~/.bashrc 中。它允许您在执行 docker run 命令时不重复该标志:
Add this snipped to your ~/.zshrc and ~/.bashrc. It allows you not to repeat the flag anytime you perform a docker run command:
您可以设置环境变量
DOCKER_DEFAULT_PLATFORM
:You can set the environment variable
DOCKER_DEFAULT_PLATFORM
:使用 Apple Silicon(或其他基于 ARM64 的架构)构建的 Docker 映像在将映像部署到 Linux 或 Windows 时可能会出现问题/em> 基于 *AMD64 环境(例如 AWS EC2、ECS 等)。例如,您可能尝试将在 M1 芯片上制作的 docker 映像上传到 AWS ECR 存储库,但它无法运行。因此,您需要一种在 ARM64 架构上构建基于 AMD64 的映像的方法,无论是使用 Docker 构建(针对单个映像)还是
docker-compose
构建(例如,对于在docker compose网络中运行的多图像应用程序)。用于构建单个 docker 镜像:
使用命令行设置环境变量或按照接受的答案中的建议修改
.bashrc
或.zshenv
文件。或者,在
Dockerfile
中,在FROM
命令中包含以下标志(对于多阶段Dockerfile
构建,仅需要该标志第一阶段):要将镜像构建为 docker-compose 构建的一部分,请为每个服务包含
platform: linux/amd64
。例如: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.Alternatively, in the
Dockerfile
, include the following flag in theFROM
command (for a multi-stageDockerfile
build, the flag is only needed for the first stage):For building images as part of a
docker-compose
build, include theplatform: linux/amd64
for each service. For example:您不需要像答案之一中提到的那样导出 env 变量,您可以通过执行以下操作将其作为命令的一部分运行:
请记住,如果您已经下载了不同平台的映像,无论您将哪个平台指定为默认平台,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:
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.您可以使用支持 cli 平台的
buildx
(mobi)。You can use
buildx
(mobi) which suipport cli for platform.您可以
在 Mac M1 的 .zshrc 文件中进行设置
you can set
in a .zshrc file for Mac M1