docker buildx跨架构构建(x86_64构建aarch64镜像)
简介
Docker Buildx 是 Docker 团队为 Docker 引擎设计的一个工具,用于支持构建和管理多平台的容器镜像。它扩展了 Docker 构建工具,使得可以跨多个不同的架构和操作系统进行构建。
第一步 先交叉编译一个aarch64的HelloWorld程序。
准备一个用于跨架构的Dockerfile文件
FROM ubuntu:20.04
COPY main /app/main
WORKDIR /app
CMD ["/app/main"]
使用docker buildx命令构建aarch64架构的镜像。
docker buildx build --platform linux/arm64 -t test:0.1 --load .
# root @ liuyuelong-System-Product-Name in ~/workspace/cross_test/src/main on git:master x [17:28:46]
$ docker buildx build --platform linux/arm64 -t aarch64_images:1.0 --load .
[+] Building 3.4s (9/9) FINISHED docker-container:elegant_davinci
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 111B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:20.04 2.8s
=> [internal] load build context 0.0s
=> => transferring context: 14.27kB 0.0s
=> CACHED [1/3] FROM docker.io/library/ubuntu:20.04@sha256:ed4a42283d9943135ed87d4ee34e542f7f5ad9ecf2f244870e23122f703f91c2 0.0s
=> => resolve docker.io/library/ubuntu:20.04@sha256:ed4a42283d9943135ed87d4ee34e542f7f5ad9ecf2f244870e23122f703f91c2 0.0s
=> [2/3] COPY main /app/main 0.0s
=> [3/3] WORKDIR /app 0.0s
=> exporting to oci image format 0.3s
=> => exporting layers 0.2s
=> => exporting manifest sha256:9354e37bfcc69a2408f10323d76afaa40e96fb04f5ef6260d23ca6f17ec86dad 0.0s
=> => exporting config sha256:a858b4c183e0ebdc2e8b217c5557e1f3fb3aebc3df98a0f8cfd5b68673005052 0.0s
=> => sending tarball 0.1s
=> importing to docker 0.0s
# root @ liuyuelong-System-Product-Name in ~/workspace/cross_test/src/main on git:master x [17:31:22]
$ docker images | grep aarch64_images
aarch64_images 1.0 a858b4c183e0 43 seconds ago 65.7MB
查看镜像具体详细信息(确定镜像是ARM架构)
# root @ liuyuelong-System-Product-Name in ~/workspace/cross_test/src/main on git:master x [17:32:10]
$ docker inspect aarch64_images:1.0
[
{
"Id": "sha256:a858b4c183e0ebdc2e8b217c5557e1f3fb3aebc3df98a0f8cfd5b68673005052",
"RepoTags": [
"aarch64_images:1.0"
],
"RepoDigests": [],
"Parent": "",
"Comment": "buildkit.dockerfile.v0",
"Created": "2023-11-30T09:31:22.44138137Z",
"Container": "",
"ContainerConfig": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": null,
"Cmd": null,
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"DockerVersion": "",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/app/main"
],
"ArgsEscaped": true,
"Image": "",
"Volumes": null,
"WorkingDir": "/app",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "20.04"
}
},
"Architecture": "arm64",
"Os": "linux",
"Size": 65692279,
"VirtualSize": 65692279,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/31cdebbee3d9a5cb5222804c1ca5759ce9d9dacc1aa94c9ff5317fe13989aca1/diff",
"MergedDir": "/var/lib/docker/overlay2/6c1bad2064ca4947f86e3a9438c25e7c926cb7d4d65c3cececcd0ce7236ec3cc/merged",
"UpperDir": "/var/lib/docker/overlay2/6c1bad2064ca4947f86e3a9438c25e7c926cb7d4d65c3cececcd0ce7236ec3cc/diff",
"WorkDir": "/var/lib/docker/overlay2/6c1bad2064ca4947f86e3a9438c25e7c926cb7d4d65c3cececcd0ce7236ec3cc/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:701ccb11c0525a289459678e1ef3e9269b62737fafb033db91061ae53a0e9c1e",
"sha256:2ebde9a9a90f240d14d223cb6d6ac61e209f0e871973ceb6c421b3b0569c09d8"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
"Architecture": "arm64",
架构没有问题
测试(通过)
导出镜像
docker save aarch64_images:1.0 -o aarch64_images.img
在aarch64架构下安装镜像
docker load -i aarch64_images.img
运行测试
# docker run --rm aarch64_images:1.0
Hello World!
小技巧(避免每次下载aarch64架构的基础镜像)
docker buildx create --use
create => Create a new builder instance 创建一个新的构造器实例
–use => Set the current builder instance 使用创建的构建器实例作为当前的构造器
创建实例后,如果使用的话,构造器就会被激活,并以容器的形式运行,在这个容器中,在构建过程中下载的基础镜像会被保存在容器中,再次构建,不需要重新下载基础镜像。
原文地址:https://blog.csdn.net/yjkhtddx/article/details/134717581
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_39132.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!