【Docker系列】 Docker 多架构支持 和 Docker Buildx 构建多架构
Docker 的多架构支持docker自适应了CPU的架构会自动选择架构的版本镜像多架构的标签,会自动选择适合的标签。使用 buildx 构建多架构镜像Windows和Mac的桌面版Docker自带buildx命令,但是Linux环境下的Docker需要自行安装buildx 。https://github.com/docker/buildxhttps://docs.docker.com/build
Docker 的多架构支持
docker自适应了CPU的架构
会自动选择架构的版本
镜像
多架构的标签,会自动选择适合的标签。
使用 buildx 构建多架构镜像
Windows和Mac的桌面版Docker自带buildx命令,但是Linux环境下的Docker需要自行安装buildx 。https://github.com/docker/buildx
https://docs.docker.com/buildx/working-with-buildx/
Docker Buildx
PS C:\Users\柏杉\Downloads\flask-redis-master> docker buildx
Usage: docker buildx [OPTIONS] COMMAND
Extended build capabilities with BuildKit
Options:
--builder string Override the configured builder instance
Management Commands:
imagetools Commands to work on images in registry
Commands:
bake Build from a file
build Start a build
create Create a new builder instance
du Disk usage
inspect Inspect current builder instance
ls List builder instances
prune Remove build cache
rm Remove a builder instance
stop Stop builder instance
use Set the current builder instance
version Show buildx version information
Run 'docker buildx COMMAND --help' for more information on a command.
登录dockerhub
docker login
进入源码目录(Dockerfile所在目录)
git clone https://github.com/xiaopeng163/flask-redis
cd flask-redis
构建 docker buildx build
--platform
支持哪些架构
-t
指tag版本,账号 / 镜像文件
注意:
yebax/flask-redis
yebax
是我的docker hub账号,flask-redis
是镜像,根据自己的账号修改名称。别上传到我的账号里了!
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 -t yebax/flask-redis:latest .
遇到问题buildx问题
因为使用Windows有两个环境,一个是desktop-linux
,一个是default
PS C:\Users\柏杉\Downloads\flask-redis-master> docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 -t yeabx/flask-redis:latest .
[+] Building 0.0s (0/0)
error: multiple platforms feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use")
PS C:\Users\柏杉\Downloads\flask-redis-master> docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS
desktop-linux docker
desktop-linux desktop-linux running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
default * docker
default default running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
创建一个新的环境 docker buildx create
--use
切换到环境里面,默认是在default
docker buildx create --name mybuilder --use
查看 docker buildx ls
PS C:\Users\柏杉\Downloads\flask-redis-master> docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS PLATFORMS
mybuilder * docker-container
mybuilder0 npipe:./pipe/docker_engine inactive
desktop-linux docker
desktop-linux desktop-linux running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
default docker
default default running linux/amd64, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
PS C:\Users\柏杉\Downloads\flask-redis-master>
注意:
*
代表激活的当前环境,例如上面的mybuilder *
再次构建
原理,拉取一个镜像,在镜像里面构建linux/arm/v7,linux/arm64/v8,linux/amd64
镜像,最后提交到docker hub 上。
PS C:\Users\柏杉\Downloads\flask-redis-master> docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 -t yebax/flask-redis:latest .
[+] Building 12.2s (20/21)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 318B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [linux/amd64 internal] load metadata for docker.io/library/python:3-slim 6.0s
=> [linux/arm64 internal] load metadata for docker.io/library/python:3-slim 6.0s
=> [linux/arm/v7 internal] load metadata for docker.io/library/python:3-slim 6.0s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [linux/arm/v7 1/4] FROM docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.0s
=> => resolve docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 28B 0.0s
=> [linux/arm64 1/4] FROM docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.1s
=> => resolve docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.1s
=> [linux/amd64 1/4] FROM docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.1s
=> => resolve docker.io/library/python:3-slim@sha256:44f51ba4af570481a83972fc424f8c1546a800341aebdbc6849a523eece02eb9 0.0s
=> CACHED [linux/arm64 2/4] RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src 0.0s
=> CACHED [linux/arm64 3/4] COPY app.py /src/app.py 0.0s
=> CACHED [linux/arm64 4/4] WORKDIR /src 0.0s
=> CACHED [linux/amd64 2/4] RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src 0.0s
=> CACHED [linux/amd64 3/4] COPY app.py /src/app.py 0.0s
=> CACHED [linux/amd64 4/4] WORKDIR /src 0.0s
=> CACHED [linux/arm/v7 2/4] RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src 0.0s
=> CACHED [linux/arm/v7 3/4] COPY app.py /src/app.py 0.0s
=> CACHED [linux/arm/v7 4/4] WORKDIR /src 0.0s
=> exporting to image 6.0s
=> => exporting layers 0.0s
=> => exporting manifest sha256:587e1270a72a7d79fc876c5008b395d4c8f5d188c8436ad4ef3544ff6a5e3ff1 0.0s
=> => exporting config sha256:bd1b0007330d0376080bafd604fcfc38fe7f6d7e02cbae506e81fcf2a62806fb 0.0s
=> => exporting manifest sha256:4fb110df2507a812560f8b273d408cde68028ad61a5463e466875db8ecf3e820 0.0s
=> => exporting config sha256:ffc79b5de755e69d57dbd979aa3e346e457ff9415f15ec6fa1d495b2fb9b0df7 0.0s
=> => exporting manifest sha256:86be7ac77e67a9d0a267c2368e30f1c30a4f24d218054a3f217aabbcc8bbb771 0.0s
=> => exporting config sha256:075647041b6d524b616d2aec1b33862d7d31a47b348532a1ee81b9e1be1848e1 0.0s
=> => exporting manifest list sha256:5d0f37e811253261018c333cd8af7723277b7000211499349c22b26fcd8c963c 0.0s
=> => pushing layers 5.8s
=> [auth] yebax/flask-redis:pull,push token for registry-1.docker.io 0.0s
可以再打开一个Powershell,输入 docker container ls
查看,出自动出现一个容器,用来构多架构建镜像。
PS C:\Users\柏杉\Downloads\flask-redis-master> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54573517ab80 moby/buildkit:buildx-stable-1 "buildkitd" 7 seconds ago Up 5 seconds buildx_buildkit_mybuilder0
上传成功,tag里面有多架构版本。
更多推荐
所有评论(0)