同时构建出x86_64和ARM64平台镜像

准备环境

# apt install -y qemu-user-static binfmt-support

配置多平台CPU指令集模拟器

# docker run --rm --privileged multiarch/qemu-user-static:register

在 x86_64(amd64)架构的服务器上,通过 QEMU 模拟运行了一个原生的 ARM64(aarch64)容器。

root@harbor:~# docker pull --platform linux/arm64 arm64v8/ubuntu:20.04
20.04: Pulling from arm64v8/ubuntu
ecd83b6c3544: Pull complete 
Digest: sha256:0908a765aeb02fe4e564b543eaed1d77839b7a08de94041e24328b2cb62ac553
Status: Downloaded newer image for arm64v8/ubuntu:20.04
docker.io/arm64v8/ubuntu:20.04
root@harbor:~# docker run --rm -it arm64v8/ubuntu:20.04 uname -a
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v2) and no specific platform was requested
Linux a856a35ad247 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux
root@harbor:~# docker run --rm -it arm64v8/ubuntu:20.04 bash 
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v2) and no specific platform was requested
root@ad0368ee7963:/# uname -a
Linux ad0368ee7963 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux

创建一个新的构建器实例

# docker buildx create --name mybuild
mybuild

切换构建器

# docker buildx use mybuild

检查当前构建器状态并确保已经启动 和初始化

# docker buildx inspect --bootstrap
[+] Building 15.4s (1/1) FINISHED                                                                                                              
 => [internal] booting buildkit                                                                                                          15.4s
 => => pulling image moby/buildkit:buildx-stable-1                                                                                       14.8s
 => => creating container buildx_buildkit_mybuild0                                                                                        0.5s
Name:          mybuild
Driver:        docker-container
Last Activity: 2025-04-22 09:34:49 +0000 UTC

Nodes:
Name:                  mybuild0
Endpoint:              unix:///var/run/docker.sock
Status:                running
BuildKit daemon flags: --allow-insecure-entitlement=network.host
BuildKit version:      v0.20.2
Platforms:             linux/amd64, linux/amd64/v2, linux/arm64, linux/riscv64, linux/ppc64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Labels:
 org.mobyproject.buildkit.worker.executor:         oci
 org.mobyproject.buildkit.worker.hostname:         3214366f824c
 org.mobyproject.buildkit.worker.network:          host
 org.mobyproject.buildkit.worker.oci.process-mode: sandbox
 org.mobyproject.buildkit.worker.selinux.enabled:  false
 org.mobyproject.buildkit.worker.snapshotter:      overlayfs
GC Policy rule#0:
 All:            false
 Filters:        type==source.local,type==exec.cachemount,type==source.git.checkout
 Keep Duration:  48h0m0s
 Max Used Space: 488.3MiB
GC Policy rule#1:
 All:            false
 Keep Duration:  1440h0m0s
 Reserved Space: 3.725GiB
 Max Used Space: 23.28GiB
 Min Free Space: 6.519GiB
GC Policy rule#2:
 All:            false
 Reserved Space: 3.725GiB
 Max Used Space: 23.28GiB
 Min Free Space: 6.519GiB
GC Policy rule#3:
 All:            true
 Reserved Space: 3.725GiB
 Max Used Space: 23.28GiB
 Min Free Space: 6.519GiB

查看构建环境

# docker buildx inspect

构建镜像试试

# cat Dockerfile 
#自定义Centos 基础镜像
FROM centos:7.9.2009 
MAINTAINER YZY

ADD filebeat-7.12.1-x86_64.rpm /tmp
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*.repo && \
    sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo && \
    yum install -y /tmp/filebeat-7.12.1-x86_64.rpm vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop && \
    rm -rf /etc/localtime /tmp/filebeat-7.12.1-x86_64.rpm && \
    ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# cat build-command.sh 
#!/bin/bash
docker buildx build -t  registry.cn-beijing.aliyuncs.com/yzybaseimages/baseimages:7.9.2009 --platform linux/amd64,linux/arm64 . --push
# bash build-command.sh

 这个是我构建成功的镜像,以后在下载镜像的时候如果是 arm 就使用 arm 的镜像,如果是 amd 就使用 amd 镜像

posted @ 2025-04-23 09:31  Maniana  阅读(172)  评论(0)    收藏  举报