使用 docker buildx 构建多 CPU 架构镜像

本文介绍了如何在 Docker Desktop 上利用 buildx 实现跨平台的 Docker 镜像构建。内容涵盖启用 Docker 的实验特性、创建新的 builder 以支持多架构构建,以及通过编写 Dockerfile 进行多架构镜像的构建和推送。通过这种方式,可以在 X86 环境下构建适用于 ARM 架构的 Docker 镜像,简化了多平台应用部署的工作流程。
摘要由CSDN通过智能技术生成

在工作中,遇到了需要将应用程序打包成 Docker 镜像并同时运行在不同的 CPU 架构(X86 和 ARM)的环境中。

ARM 架构与 X86 相比,ARM 低功耗、移动市场占比高,X86 高性能、服务器市场占比高。

不同的 CPU 架构,对于运行相同的应用程序的 Docker 容器,需要分别在相应的 CPU 架构下编译的 Docker 镜像。
要构建多架构镜像,首先想到的是每种 CPU 架构环境(物理环境或虚拟环境)下构建相应的镜像。
但目前 docker 构建环境是 X86 的,没有 ARM 环境,
或者要申请 ARM 物理机,或者要申请/创建 ARM 虚拟机,或者交叉编辑等等,听上去都比较麻烦。

经过研究,发现 docker buildx 支持构建多架构镜像,这使得构建多架构镜像变得简单。
这样就可以在 X86 架构下构建 ARM 架构的镜像。

接下来,开始实践之旅吧。

环境
Docker Desktop(Mac)

Docker Engine 19.03+

实践步骤
第一步,开启 docker buildx
docker buildx 目前还是试验功能,默认没有开启,需要在 Docker Desktop 的首选项中开启它

Docker —>Preferences —>Command Line —> Enable experimental features

执行 docker buildx 命令,输出截图如下:

第二步,构建多架构镜像,并推送到 Docker Hub
写个简单的 Dockerfile,仅做演示:

echo "FROM python:3.7-alpine”>Dockerfile

列出 builder:

$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS  PLATFORMS
default * docker
  default default         running linux/amd64, linux/arm64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
我们当前使用的是默认的 builder,它基本上是旧的 builder。
让我们创建一个新的 builder,它使我们能够访问一些新的多架构结构功能。

https://www.imdb.com/list/ls080139814/?publish=publish
https://www.imdb.com/list/ls080139818/?publish=publish
https://www.imdb.com/list/ls080139831/?publish=publish
https://www.imdb.com/list/ls080139832/?publish=publish
https://www.imdb.com/list/ls080139867/?publish=publish
https://www.imdb.com/list/ls080139863/?publish=publish
https://www.imdb.com/list/ls080139864/?publish=publish
https://www.imdb.com/list/ls080139827/?publish=publish
https://www.imdb.com/list/ls080139829/?publish=publish
https://www.imdb.com/list/ls080139843/?publish=publish
https://www.imdb.com/list/ls080139842/?publish=publish
https://www.imdb.com/list/ls080139897/?publish=publish
https://www.imdb.com/list/ls080139893/?publish=publish
https://www.imdb.com/list/ls080139880/?publish=publish
https://www.imdb.com/list/ls080139887/?publish=publish
https://www.imdb.com/list/ls080138571/?publish=publish
https://www.imdb.com/list/ls080138572/?publish=publish
https://www.imdb.com/list/ls080138578/?publish=publish
https://www.imdb.com/list/ls080138517/?publish=publish
https://www.imdb.com/list/ls080138514/?publish=publish
https://www.imdb.com/list/ls080138530/?publish=publish
https://www.imdb.com/list/ls080138536/?publish=publish
https://www.imdb.com/list/ls080138567/?publish=publish
https://www.imdb.com/list/ls080138527/?publish=publish
https://www.imdb.com/list/ls080138529/?publish=publish
https://www.imdb.com/list/ls080155334/?publish=publish
https://www.imdb.com/list/ls080155369/?publish=publish
https://www.imdb.com/list/ls080155321/?publish=publish
https://www.imdb.com/list/ls080155348/?publish=publish
https://www.imdb.com/list/ls080155606/?publish=publish
https://www.imdb.com/list/ls080200255/?publish=publish
https://www.imdb.com/list/ls080200232/?publish=publish
https://www.imdb.com/list/ls080200284/?publish=publish
https://www.imdb.com/list/ls080200463/?publish=publish
https://www.imdb.com/list/ls080200906/?publish=publish
https://www.imdb.com/list/ls080200932/?publish=publish
https://www.imdb.com/list/ls080200988/?publish=publish
https://www.imdb.com/list/ls080200865/?publish=publish
https://www.imdb.com/list/ls080205009/?publish=publish
https://www.imdb.com/list/ls080205039/?publish=publish
https://www.imdb.com/list/ls080192014/?publish=publish
https://www.imdb.com/list/ls080192034/?publish=publish
https://www.imdb.com/list/ls080192028/?publish=publish
https://www.imdb.com/list/ls080192091/?publish=publish
https://www.imdb.com/list/ls080192507/?publish=publish
https://www.imdb.com/list/ls080192574/?publish=publish
https://www.imdb.com/list/ls080192533/?publish=publish
https://www.imdb.com/list/ls080192522/?publish=publish
https://www.imdb.com/list/ls080192540/?publish=publish
https://www.imdb.com/list/ls080192596/?publish=publish
https://www.imdb.com/list/ls080192754/?publish=publish
https://www.imdb.com/list/ls080192778/?publish=publish
https://www.imdb.com/list/ls080192739/?publish=publish
https://www.imdb.com/list/ls080192721/?publish=publish
https://www.imdb.com/list/ls080192746/?publish=publish
https://www.imdb.com/list/ls080192780/?publish=publish
https://www.imdb.com/list/ls080192106/?publish=publish
https://www.imdb.com/list/ls080192177/?publish=publish
https://www.imdb.com/list/ls080192114/?publish=publish
https://www.imdb.com/list/ls080192127/?publish=publish
https://www.imdb.com/list/ls080192191/?publish=publish
https://www.imdb.com/list/ls080192304/?publish=publish
https://www.imdb.com/list/ls080192373/?publish=publish
https://www.imdb.com/list/ls080192319/?publish=publish
https://www.imdb.com/list/ls080192326/?publish=publish
https://www.imdb.com/list/ls080192397/?publish=publish
https://www.imdb.com/list/ls080192382/?publish=publish
https://www.imdb.com/list/ls080192655/?publish=publish
https://www.imdb.com/list/ls080192619/?publish=publish
https://www.imdb.com/list/ls080192632/?publish=publish
https://www.imdb.com/list/ls080192629/?publish=publish
https://www.imdb.com/list/ls080192685/?publish=publish
https://www.imdb.com/list/ls080192209/?publish=publish
https://www.imdb.com/list/ls080192277/?publish=publish
https://www.imdb.com/list/ls080192211/?publish=publish
https://www.imdb.com/list/ls080192235/?publish=publish
https://www.imdb.com/list/ls080192268/?publish=publish
https://www.imdb.com/list/ls080192241/?publish=publish
https://www.imdb.com/list/ls080192296/?publish=publish
https://www.imdb.com/list/ls080192450/?publish=publish
https://www.imdb.com/list/ls080192476/?publish=publish
https://www.imdb.com/list/ls080192433/?publish=publish
https://www.imdb.com/list/ls080192490/?publish=publish
https://www.imdb.com/list/ls080192904/?publish=publish
https://www.imdb.com/list/ls080192917/?publish=publish
https://www.imdb.com/list/ls080192921/?publish=publish
https://www.imdb.com/list/ls080192997/?publish=publish
https://www.imdb.com/list/ls080192807/?publish=publish
https://www.imdb.com/list/ls080192852/?publish=publish
https://www.imdb.com/list/ls080192813/?publish=publish
https://www.imdb.com/list/ls080192860/?publish=publish
https://www.imdb.com/list/ls080192823/?publish=publish
https://www.imdb.com/list/ls080192898/?publish=publish
https://www.imdb.com/list/ls080194001/?publish=publish
https://www.imdb.com/list/ls080194076/?publish=publish
https://www.imdb.com/list/ls080194036/?publish=publish
https://www.imdb.com/list/ls080194044/?publish=publish
https://www.imdb.com/list/ls080194082/?publish=publish
https://www.imdb.com/list/ls080194556/?publish=publish
https://www.imdb.com/list/ls080194519/?publish=publish
https://www.imdb.com/list/ls080194528/?publish=publish
https://www.imdb.com/list/ls080194599/?publish=publish
https://www.imdb.com/list/ls080194706/?publish=publish
https://www.imdb.com/list/ls080194771/?publish=publish
https://www.imdb.com/list/ls080194712/?publish=publish
https://www.imdb.com/list/ls080194764/?publish=publish
https://www.imdb.com/list/ls080194748/?publish=publish
https://www.imdb.com/list/ls080194786/?publish=publish
https://www.imdb.com/list/ls080194151/?publish=publish
https://www.imdb.com/list/ls080194111/?publish=publish
https://www.imdb.com/list/ls080194161/?publish=publish
https://www.imdb.com/list/ls080194143/?publish=publish
https://www.imdb.com/list/ls080194180/?publish=publish
https://www.imdb.com/list/ls080194357/?publish=publish
https://www.imdb.com/list/ls080194372/?publish=publish
https://www.imdb.com/list/ls080194368/?publish=publish
https://www.imdb.com/list/ls080194342/?publish=publish
https://www.imdb.com/list/ls080194386/?publish=publish
https://www.imdb.com/list/ls080194651/?publish=publish
https://www.imdb.com/list/ls080194612/?publish=publish
https://www.imdb.com/list/ls080194660/?publish=publish
https://www.imdb.com/list/ls080194629/?publish=publish
https://www.imdb.com/list/ls080194696/?publish=publish
https://www.imdb.com/list/ls080194682/?publish=publish
https://www.imdb.com/list/ls080194277/?publish=publish
https://www.imdb.com/list/ls080194237/?publish=publish
https://www.imdb.com/list/ls080194223/?publish=publish
https://www.imdb.com/list/ls080194249/?publish=publish
https://www.imdb.com/list/ls080194406/?publish=publish
https://www.imdb.com/list/ls080194476/?publish=publish
https://www.imdb.com/list/ls080194430/?publish=publish
https://www.imdb.com/list/ls080194434/?publish=publish
https://www.imdb.com/list/ls080194426/?publish=publish
https://www.imdb.com/list/ls080194497/?publish=publish
https://www.imdb.com/list/ls080194486/?publish=publish
https://www.imdb.com/list/ls080194956/?publish=publish
https://www.imdb.com/list/ls080194913/?publish=publish
https://www.imdb.com/list/ls080194938/?publish=publish
https://www.imdb.com/list/ls080194928/?publish=publish
https://www.imdb.com/list/ls080194993/?publish=publish
https://www.imdb.com/list/ls080194806/?publish=publish
https://www.imdb.com/list/ls080194810/?publish=publish
https://www.imdb.com/list/ls080194820/?publish=publish
https://www.imdb.com/list/ls080194848/?publish=publish
https://www.imdb.com/list/ls080194899/?publish=publish
https://www.imdb.com/list/ls080199052/?publish=publish
https://www.imdb.com/list/ls080199013/?publish=publish
https://www.imdb.com/list/ls080199020/?publish=publish
https://www.imdb.com/list/ls080199043/?publish=publish
https://www.imdb.com/list/ls080199096/?publish=publish
https://www.imdb.com/list/ls080193460/?publish=publish
https://www.imdb.com/list/ls080193429/?publish=publish
https://www.imdb.com/list/ls080193486/?publish=publish
https://www.imdb.com/list/ls080193904/?publish=publish
https://www.imdb.com/list/ls080193977/?publish=publish
https://www.imdb.com/list/ls080193914/?publish=publish
https://www.imdb.com/list/ls080193964/?publish=publish
https://www.imdb.com/list/ls080193924/?publish=publish
https://www.imdb.com/list/ls080193996/?publish=publish
https://www.imdb.com/list/ls080193981/?publish=publish
https://www.imdb.com/list/ls080193804/?publish=publish
https://www.imdb.com/list/ls080193873/?publish=publish
https://www.imdb.com/list/ls080193812/?publish=publish
https://www.imdb.com/list/ls080193831/?publish=publish
https://www.imdb.com/list/ls080193866/?publish=publish
https://www.imdb.com/list/ls080193845/?publish=publish
https://www.imdb.com/list/ls080193896/?publish=publish
https://www.imdb.com/list/ls080196007/?publish=publish
https://www.imdb.com/list/ls080196058/?publish=publish
https://www.imdb.com/list/ls080196078/?publish=publish
https://www.imdb.com/list/ls080196033/?publish=publish
https://www.imdb.com/list/ls080196027/?publish=publish
https://www.imdb.com/list/ls080196042/?publish=publish
https://www.imdb.com/list/ls080196503/?publish=publish
https://www.imdb.com/list/ls080196556/?publish=publish
https://www.imdb.com/list/ls080196513/?publish=publish
https://www.imdb.com/list/ls080196539/?publish=publish
https://www.imdb.com/list/ls080196521/?publish=publish
https://www.imdb.com/list/ls080196592/?publish=publish
https://www.imdb.com/list/ls080196709/?publish=publish
https://www.imdb.com/list/ls080196775/?publish=publish
https://www.imdb.com/list/ls080196735/?publish=publish
https://www.imdb.com/list/ls080196760/?publish=publish
https://www.imdb.com/list/ls080196722/?publish=publish
https://www.imdb.com/list/ls080196796/?publish=publish
https://www.imdb.com/list/ls080196103/?publish=publish
https://www.imdb.com/list/ls080196176/?publish=publish
https://www.imdb.com/list/ls080196133/?publish=publish
https://www.imdb.com/list/ls080196123/?publish=publish
https://www.imdb.com/list/ls080196149/?publish=publish
https://www.imdb.com/list/ls080196306/?publish=publish
https://www.imdb.com/list/ls080196359/?publish=publish
https://www.imdb.com/list/ls080196315/?publish=publish
https://www.imdb.com/list/ls080196333/?publish=publish
https://www.imdb.com/list/ls080196365/?publish=publish
https://www.imdb.com/list/ls080196343/?publish=publish
https://www.imdb.com/list/ls080196387/?publish=publish
https://www.imdb.com/list/ls080196608/?publish=publish
https://www.imdb.com/list/ls080196678/?publish=publish
https://www.imdb.com/list/ls080196639/?publish=publish
https://www.imdb.com/list/ls080678621/?publish=publish
https://www.imdb.com/list/ls080678697/?publish=publish
https://www.imdb.com/list/ls080678686/?publish=publish
https://www.imdb.com/list/ls080678270/?publish=publish
https://www.imdb.com/list/ls080678211/?publish=publish
https://www.imdb.com/list/ls080678261/?publish=publish
https://www.imdb.com/list/ls080678240/?publish=publish
https://www.imdb.com/list/ls080678281/?publish=publish
https://www.imdb.com/list/ls080678457/?publish=publish
https://www.imdb.com/list/ls080678479/?publish=publish
https://www.imdb.com/list/ls080678460/?publish=publish
https://www.imdb.com/list/ls080678426/?publish=publish
https://www.imdb.com/list/ls080678495/?publish=publish
https://www.imdb.com/list/ls080678950/?publish=publish
https://www.imdb.com/list/ls080678917/?publish=publish
https://www.imdb.com/list/ls080678962/?publish=publish
https://www.imdb.com/list/ls080678944/?publish=publish
https://www.imdb.com/list/ls080678801/?publish=publish
https://www.imdb.com/list/ls080678871/?publish=publish
https://www.imdb.com/list/ls080678866/?publish=publish
https://www.imdb.com/list/ls080678847/?publish=publish
https://www.imdb.com/list/ls080678898/?publish=publish
https://www.imdb.com/list/ls080610070/?publish=publish
https://www.imdb.com/list/ls080610011/?publish=publish
https://www.imdb.com/list/ls080610061/?publish=publish
https://www.imdb.com/list/ls080610028/?publish=publish
https://www.imdb.com/list/ls080610098/?publish=publish
https://www.imdb.com/list/ls080610504/?publish=publish
https://www.imdb.com/list/ls080610510/?publish=publish
https://www.imdb.com/list/ls080610560/?publish=publish
https://www.imdb.com/list/ls080610545/?publish=publish
https://www.imdb.com/list/ls080610584/?publish=publish
https://www.imdb.com/list/ls080610759/?publish=publish
https://www.imdb.com/list/ls080610717/?publish=publish
https://www.imdb.com/list/ls080610736/?publish=publish
https://www.imdb.com/list/ls080610720/?publish=publish
https://www.imdb.com/list/ls080610780/?publish=publish
https://www.imdb.com/list/ls080610151/?publish=publish
https://www.imdb.com/list/ls080610119/?publish=publish
https://www.imdb.com/list/ls080610166/?publish=publish
https://www.imdb.com/list/ls080679079/?publish=publish
https://www.imdb.com/list/ls080679039/?publish=publish
https://www.imdb.com/list/ls080679043/?publish=publish
https://www.imdb.com/list/ls080179704/?mode=desktop
https://www.imdb.com/list/ls080179753/?mode=desktop
https://www.imdb.com/list/ls080179770/?mode=desktop
https://www.imdb.com/list/ls080179772/?mode=desktop
https://www.imdb.com/list/ls080179715/?mode=desktop
https://www.imdb.com/list/ls080178533/?mode=desktop
https://www.imdb.com/list/ls080178567/?mode=desktop
https://www.imdb.com/list/ls080178527/?mode=desktop
https://www.imdb.com/list/ls080178547/?mode=desktop
https://www.imdb.com/list/ls080178593/?mode=desktop
https://www.imdb.com/list/ls080139875/?mode=desktop
https://www.imdb.com/list/ls080139873/?mode=desktop
https://www.imdb.com/list/ls080139876/?mode=desktop
https://www.imdb.com/list/ls080139810/?mode=desktop
https://www.imdb.com/list/ls080139813/?mode=desktop
https://www.imdb.com/list/ls080139814/?mode=desktop
https://www.imdb.com/list/ls080139818/?mode=desktop
https://www.imdb.com/list/ls080139831/?mode=desktop
https://www.imdb.com/list/ls080139832/?mode=desktop
https://www.imdb.com/list/ls080139867/?mode=desktop
https://www.imdb.com/list/ls080139863/?mode=desktop
https://www.imdb.com/list/ls080139864/?mode=desktop
https://www.imdb.com/list/ls080139827/?mode=desktop
https://www.imdb.com/list/ls080139829/?mode=desktop
https://www.imdb.com/list/ls080139843/?mode=desktop
https://www.imdb.com/list/ls080139842/?mode=desktop
https://www.imdb.com/list/ls080139897/?mode=desktop
https://www.imdb.com/list/ls080139893/?mode=desktop
https://www.imdb.com/list/ls080139880/?mode=desktop
https://www.imdb.com/list/ls080139887/?mode=desktop
https://www.imdb.com/list/ls080138571/?mode=desktop
https://www.imdb.com/list/ls080138572/?mode=desktop
https://www.imdb.com/list/ls080138578/?mode=desktop
https://www.imdb.com/list/ls080138517/?mode=desktop
https://www.imdb.com/list/ls080138514/?mode=desktop
https://www.imdb.com/list/ls080138530/?mode=desktop
https://www.imdb.com/list/ls080138536/?mode=desktop
https://www.imdb.com/list/ls080138567/?mode=desktop
https://www.imdb.com/list/ls080138527/?mode=desktop
https://www.imdb.com/list/ls080138529/?mode=desktop
https://www.imdb.com/list/ls080155334/?mode=desktop
https://www.imdb.com/list/ls080155369/?mode=desktop
https://www.imdb.com/list/ls080155321/?mode=desktop
https://www.imdb.com/list/ls080199194/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199353/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199336/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199608/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199408/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199481/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199975/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080199965/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198510/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198526/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198592/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198347/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198389/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198613/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198975/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198962/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080198854/?mode=desktop&ref_=m_ft_dsk
https://www.imdb.com/list/ls080110040/?mode=desktop
https://www.imdb.com/list/ls080110537/?mode=desktop
https://www.imdb.com/list/ls080114931/?mode=desktop
https://www.imdb.com/list/ls080119023/?mode=desktop
https://www.imdb.com/list/ls080119668/?mode=desktop
https://www.imdb.com/list/ls080119210/?mode=desktop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值