跳转到主要内容

pull&&retag&&push


#!/usr/bin/env python3
import subprocess
import sys


def pull_rename_push_image(image_name, target_repo):
    # 拉取镜像
    subprocess.check_call(["docker", "pull", image_name])

    # 获取镜像ID
    result = subprocess.Popen(
        ["docker", "images", "-q", image_name],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
    )
    image_id = result.communicate()[0].strip()

    # 生成新的镜像名称
    new_image_name = "{}/{}".format(target_repo, image_name)

    # 给镜像打标签
    subprocess.check_call(["docker", "tag", image_id, new_image_name])

    # 推送镜像到私有仓库
    subprocess.check_call(["docker", "push", new_image_name])

    print("镜像 {} 已成功推送到 {}".format(image_name, target_repo))


def main():
    if len(sys.argv) != 2:
        print("Usage: python pull.py <image_name>")
        sys.exit(1)

    image_name = sys.argv[1]

    # 选择目标仓库
    print("请选择目标仓库:")
    print("1. harbor.iovhm.com/public")
    print("2. swr.cn-south-1.myhuaweicloud.com/vp-public")
    choice = input("请输入选项编号 (1 或 2): ")

    if choice == "1":
        target_repo = "harbor.iovhm.com/public"
    elif choice == "2":
        target_repo = "swr.cn-south-1.myhuaweicloud.com/vp-public"
    else:
        print("无效的选项")
        sys.exit(1)

    # 执行拉取、改名、推送操作
    pull_rename_push_image(image_name, target_repo)


if __name__ == "__main__":
    main()


# python3 pull.py nginx:latest