searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

[镜像]双架构镜像的3种构建方法

2023-12-12 07:09:11
11
0

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
0条评论
0 / 1000