因為國產(chǎn)化的普及,尤其一些證券和銀行行業(yè),已經(jīng)開始走信創(chuàng)的路線,后期也許會接觸到國產(chǎn) CPU (
arm
平臺,比如華為的鯤鵬處理器)自己買
arm
平臺的CPU
,這個成本著實吃不消,于是嘗試x86
平臺運行arm
平臺的容器來降本增效
關(guān)于 docker 版本
docker
運行其他平臺容器,需要使用--platform
參數(shù)來指定平臺docker 19.03.9
及以上的版本才支持--platform
參數(shù)- 默認沒有開啟
--platform
參數(shù),需要手動開啟,直接執(zhí)行,會有下面的報錯
"--platform" is only supported on a Docker daemon with experimental features enabled
查看是否開啟 experimental 功能
--platform
參數(shù)需要experimental
為true
,通過下面的命令可以驗證是否開啟
docker info | grep -i 'experimental'
開啟 experimental 功能
修改
daemon.json
文件,增加下面的參數(shù)
"experimental": true
下面的
daemon.json
文件僅供參考
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"exec-opts": ["native.cgroupdriver=systemd"],
"experimental": true,
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
修改完成后,重啟
docker
來驗證
systemctl restart docker
docker info | grep -i 'experimental'
查看當前環(huán)境平臺
uname -m
我的平臺返回下面的內(nèi)容
x86_64
拉取一個 arm 平臺的容器
docker pull --platform arm64 debian:11
查看鏡像使用的平臺
docker inspect debian:11 | grep -i 'architecture'
預(yù)期返回的結(jié)果如下
"Architecture": "arm64",
如果不加
--platform
參數(shù),默認拉取自己當前 CPU 平臺的鏡像如果本地有相同
tag
的鏡像,只是平臺不同的情況下,需要注意區(qū)分tag
,不然直接docker pull
就會覆蓋掉之前的鏡像,之前的鏡像tag
會變?yōu)?<none>
運行一個 arm 平臺的容器
在沒有
qemu-user-static
的幫助下,單靠--platform
參數(shù)是無法啟動非本機平臺的鏡像的
docker run --platform arm64 -t debian:11 uname -m
返回的報錯如下,是因為 CPU 平臺不同
standard_init_linux.go:211: exec user process caused "exec format error"
整一個 qemu-user-static
multiarch/qemu-user-static - github
注冊可支持的架構(gòu)解釋器
不指定 CPU 平臺,使用
register
來注冊可支持的架構(gòu)解析器
docker run --rm \
--privileged \
multiarch/qemu-user-static:register \
--reset
執(zhí)行成功后,會返回類似如下的結(jié)果來表明支持的架構(gòu)解析器
Setting /usr/bin/qemu-alpha-static as binfmt interpreter for alpha
Setting /usr/bin/qemu-arm-static as binfmt interpreter for arm
Setting /usr/bin/qemu-armeb-static as binfmt interpreter for armeb
Setting /usr/bin/qemu-sparc-static as binfmt interpreter for sparc
Setting /usr/bin/qemu-sparc32plus-static as binfmt interpreter for sparc32plus
Setting /usr/bin/qemu-sparc64-static as binfmt interpreter for sparc64
Setting /usr/bin/qemu-ppc-static as binfmt interpreter for ppc
Setting /usr/bin/qemu-ppc64-static as binfmt interpreter for ppc64
Setting /usr/bin/qemu-ppc64le-static as binfmt interpreter for ppc64le
Setting /usr/bin/qemu-m68k-static as binfmt interpreter for m68k
Setting /usr/bin/qemu-mips-static as binfmt interpreter for mips
Setting /usr/bin/qemu-mipsel-static as binfmt interpreter for mipsel
Setting /usr/bin/qemu-mipsn32-static as binfmt interpreter for mipsn32
Setting /usr/bin/qemu-mipsn32el-static as binfmt interpreter for mipsn32el
Setting /usr/bin/qemu-mips64-static as binfmt interpreter for mips64
Setting /usr/bin/qemu-mips64el-static as binfmt interpreter for mips64el
Setting /usr/bin/qemu-sh4-static as binfmt interpreter for sh4
Setting /usr/bin/qemu-sh4eb-static as binfmt interpreter for sh4eb
Setting /usr/bin/qemu-s390x-static as binfmt interpreter for s390x
Setting /usr/bin/qemu-aarch64-static as binfmt interpreter for aarch64
Setting /usr/bin/qemu-aarch64_be-static as binfmt interpreter for aarch64_be
Setting /usr/bin/qemu-hppa-static as binfmt interpreter for hppa
Setting /usr/bin/qemu-riscv32-static as binfmt interpreter for riscv32
Setting /usr/bin/qemu-riscv64-static as binfmt interpreter for riscv64
Setting /usr/bin/qemu-xtensa-static as binfmt interpreter for xtensa
Setting /usr/bin/qemu-xtensaeb-static as binfmt interpreter for xtensaeb
Setting /usr/bin/qemu-microblaze-static as binfmt interpreter for microblaze
Setting /usr/bin/qemu-microblazeel-static as binfmt interpreter for microblazeel
Setting /usr/bin/qemu-or1k-static as binfmt interpreter for or1k
Setting /usr/bin/qemu-hexagon-static as binfmt interpreter for hexagon
嘗試啟動 arm64 鏡像
下載
qemu-aarch64-static
wget https://github.com/multiarch/qemu-user-static/releases/download/v5.2.0-1/qemu-aarch64-static && \
chmod +x qemu-aarch64-static
啟動容器時將
qemu-aarch64-static
帶入到容器內(nèi)
注意 qemu-aarch64-static 二進制文件的路徑,可以自己歸納到指定的路徑,只需要帶入到容器內(nèi)的 /usr/bin 目錄下就好了
docker run -t \
--rm \
--platform arm64 \
-v $(pwd)/qemu-aarch64-static:/usr/bin/qemu-aarch64-static \
debian:11 \
uname -m
正常情況下,返回
aarch64
后容器就會被銷毀了(因為加了 --rm 參數(shù)
)
嘗試啟動 ppc64le 鏡像
下載
qemu-ppc64le-static
wget https://github.com/multiarch/qemu-user-static/releases/download/v5.2.0-1/qemu-ppc64le-static && \
chmod +x qemu-ppc64le-static
啟動
ppc64le
平臺的鏡像
docker run -t \
--rm \
--platform ppc64le \
-v $(pwd)/qemu-ppc64le-static:/usr/bin/qemu-ppc64le-static \
alpine:3.17.2 \
uname -m
正常情況下,返回
ppc64le
后容器就會被銷毀了(因為加了 --rm 參數(shù)
)
后臺運行 arm64 容器
sleep 315360000
讓容器在后臺睡眠 100 年(因為容器能在后臺運行的方法就是要有一個常駐前臺的進程
)
docker run -d \
--rm \
--platform arm64 \
-v $(pwd)/qemu-aarch64-static:/usr/bin/qemu-aarch64-static \
--name centos_7.9_arm64 \
centos:7.9.2009 \
sleep 315360000
進入容器
docker exec -it centos_7.9_arm64 bash
在容器內(nèi)執(zhí)行 yum 命令,可以看到一切都是正常的,并且是
aarch64
的
yum install -y vim
build 一個 arm64 鏡像
- 準備一個
Dockerfile
- 需要將
qemu-aarch64-static
帶入到容器內(nèi)的/usr/bin
目錄下才可以實現(xiàn)構(gòu)建- 不然會返回
standard_init_linux.go:211: exec user process caused "no such file or directory"
這樣的報錯
FROM centos:7.9.2009
ADD ./qemu-aarch64-static /usr/bin/qemu-aarch64-static
RUN yum install -y net-tools gcc gcc-c++ make vim && \
yum clean all
開始構(gòu)建
docker build \
--platform arm64 \
-t centos_make:7.9_aarch64 .
構(gòu)建完成后會返回兩個
Successfully
構(gòu)建完成后,可以使用下面的命令驗證容器內(nèi)的平臺是否是
arm64
docker inspect centos_make:7.9_aarch64 | grep -i 'architecture'
到這里,
x86
平臺運行和構(gòu)建其他平臺鏡像的學(xué)習就結(jié)束了,下面有一篇關(guān)于多架構(gòu)支持的文章,有興趣的可以看一看(我是真的看麻了,太難了吧,果然還是開發(fā)改變世界)文章來源:http://www.zghlxwxcb.cn/news/detail-781807.html
參考文檔
容器鏡像多架構(gòu)支持介紹文章來源地址http://www.zghlxwxcb.cn/news/detail-781807.html
到了這里,關(guān)于x86 平臺利用 qemu-user-static 實現(xiàn) arm64 平臺 docker 鏡像的運行和構(gòu)建的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!