秋栈博客

七月

批量处理某些docker镜像无法拉取或架构不适配的解决方案

7
2023-03-29

批量处理某些docker镜像无法拉取或架构不适配的解决方案

先将需要的镜像按行保存到文本中
cat > ./images.txt <

方案一:离线传输

在本地或代理服务器拉取镜像
xargs -I {} docker pull {} < images.txt
保存为tar归档文件
xargs -I {} docker save -o robust-images.tar {} < images.txt
上传到所在服务器执行加载命令
docker load -i robust-images.tar

方案2:阿里云中转

编写自动化批量处理脚本

需要提前执行docker login操作
#!/bin/bash
set -x
set -e

REGISTRY="registry.cn-hangzhou.aliyuncs.com"
REPO="kubernetes-hub"

#Read the content in the images.txt file line by line.
while read -r IMAGE; do

    # Capture the tag and name of the image
    # e.g. k8s.gcr.io/kube-apiserver:v1.18.0 to kube-apiserver:v1.18.0
    TAG=$(echo "$IMAGE" | awk -F: '{print $2}')
    NAME=$(echo "$IMAGE" | awk -F/ '{print $NF}' | awk -F: '{print $1}')
    NEW_IMAGE="$REGISTRY/$REPO/$NAME:$TAG"

    # AMD64
    if ! docker pull --platform linux/amd64 "$IMAGE"; then
        echo "pull $IMAGE failed, retry"
        if ! docker pull --platform linux/amd64 "$IMAGE"; then
            echo "pull $IMAGE failed"
            exit 1
        fi
    fi

    docker tag "$IMAGE" "$NEW_IMAGE-amd64"
    docker rmi "$IMAGE"

    # ARM64
    if ! docker pull --platform linux/arm64 "$IMAGE"; then
        echo "pull $IMAGE failed, retry"
        if ! docker pull --platform linux/arm64 "$IMAGE"; then
            echo "pull $IMAGE failed"
            exit 1
        fi
    fi

    docker tag "$IMAGE" "$NEW_IMAGE-arm64"
    docker rmi "$IMAGE"

    docker push "$NEW_IMAGE-amd64"
    docker push "$NEW_IMAGE-arm64"

    docker manifest create --insecure "$NEW_IMAGE" --amend "$NEW_IMAGE-amd64" --amend "$NEW_IMAGE-arm64"
    docker manifest push "$NEW_IMAGE"

done 
 

拉取镜像

公网:registry.cn-hangzhou.aliyuncs.com/kubernetes-hub 阿里云VPC:registry-vpc.cn-hangzhou.aliyuncs.com/kubernetes-hub 由于上面已经操作过manifest,所以不用指定架构直接拉取即可。脚本如下:
#!/bin/bash
set -x
set -e

REGISTRY="registry.cn-hangzhou.aliyuncs.com"
REPO="kubernetes-hub"

#Read the content in the images.txt file line by line.
while read -r IMAGE; do

    TAG=$(echo "$IMAGE" | awk -F: '{print $2}')
    NAME=$(echo "$IMAGE" | awk -F/ '{print $NF}' | awk -F: '{print $1}')
    NEW_IMAGE="$REGISTRY/$REPO/$NAME:$TAG"

    if ! docker pull "$NEW_IMAGE"; then
        echo "pull $NEW_IMAGE failed, retry"
        if ! docker pull "$NEW_IMAGE"; then
            echo "pull $NEW_IMAGE failed"
            exit 1
        fi
    fi

    docker tag "$NEW_IMAGE" "$IMAGE"
    docker rmi "$NEW_IMAGE"

done 
 
  • 0