1. 背景
雙架構鏡像的好處是能根據當前的系統架構(x86或者arm64), 進行自動對應版本鏡像的拉取,避免了客戶端側去做這個鏡像tag的精確選擇。
2.構建方法
2.1 從源碼構建
例如一下以go項目為例子
Dockerfile:
# Build
FROM golang:1.20 as builder
ENV GOPROXY "xxx://goproxy.cn,direct"
ENV GOPROVATE "gitlab.ctyuncdn.cn,git.fogcdn.top"
ADD . /workspace/project
WORKDIR /workspace/project
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -o helloworld main.go
FROM alpine:3.14
WORKDIR /usr/bin
COPY --from=builder /workspace/project/helloworld .
RUN chmod +x /usr/bin/helloworld
WORKDIR /home/helloworld
RUN mkdir -p /home/helloworld/log
ENTRYPOINT["/usr/bin/helloworld"]
編譯腳本:
#!/bin/bash
group=nest
registry=xxx
image=helloworld
DRONE_BRANCH=23.4.12
DRONE_BUILD_NUMBER=1
build(){
docker buildx create --use --name ${group} --node ${group}0
docker buildx build --platform=linux/arm64,linux/amd64 \
-f Dockerfile \
--push \
-t ${registry}/${image}:latest \
.
}
main(){
build $@
}
main $@
如上通過腳本就能完成雙架構的鏡像的構建和推送。 主要使用了docker buildx
2.2 手工打manifest
即基于現有的兩個架構的鏡像,手工打成一個manifest
docker manifest create --amend --insecure xx/cruise-control:3.0.4 xx/cruise-control:3.0.4-amd xx/cruise-control:3.0.3-arm
# 給鏡像manifest文件添加arch信息
docker manifest annotate xx/cruise-control:3.0.4 xx/cruise-control:3.0.4-amd --arch amd64
docker manifest annotate xx/cruise-control:3.0.4 xx/cruise-control:3.0.3-arm --arch arm64
# 向swr鏡像倉庫推送鏡像manifest
docker manifest push -p --insecure xx/cruise-control:3.0.4