docker在x86_64环境运行arm64的Alpine

更新于 2024-08-05

构建arm镜像

docker pull --platform arm64 alpine

#全平台:
docker run --privileged --rm tonistiigi/binfmt --install all

#支持 arm64:
docker run --privileged --rm tonistiigi/binfmt --install arm64

docker buildx create --name multi-arch-builder --driver docker-container --bootstrap --use

docker buildx inspect multi-arch-builder

docker buildx build --platform linux/arm64 --load .

#Dockerfile
FROM --platform=arm64 alpine:latest

启动镜像

#查看是否开启 experimental 功能
docker info | grep -i 'experimental'

#开启 experimental 功能
#/etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://dockerproxy.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://docker.nju.edu.cn"
    ],
    "experimental": true
}

#重启docker
systemctl restart docker && docker info | grep -i 'experimental'

#拉取镜像
#注意会覆盖不同构架的相同tag镜像
docker pull --platform arm64 alpine
#查看镜像
docker inspect alpine:latest | grep -i 'architecture'
#"Architecture": "arm64",

#注册qemu
docker run --rm \
--privileged \
multiarch/qemu-user-static:register \
--reset

#下载模拟器
mkdir -p /root/.qemu && \
wget https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O /root/.qemu/qemu-aarch64-static && \
chmod +x /root/.qemu/qemu-aarch64-static

#启动镜像
docker run -it --rm --platform arm64 \
-v /root/.qemu/qemu-aarch64-static:/usr/bin/qemu-aarch64-static \
alpine:latest

#容器内检测环境
$ uname -m