前一阵子搭建了一个harbor镜像仓库,并且写了搬砖脚本,只是提供一些运维用的到的镜像仓库。

如果大家有需要,可以试试。

如果需要一些运维工作相关的镜像,可以留言,我会及时同步。

镜像仓库地址:

harbor.op123.ren:44301

修改docker配置:

cat <<'EOF'>/etc/docker/daemon.json
{
  "debug": false,
  "experimental": true,
  "registry-mirrors": [ "https://harbor.op123.ren:44301" ]
}
EOF


systemctl daemon-reload
systemctl restart docker

如此一来,既方便大家使用,也可以丰富仓库内容。让运维工作更顺畅自然。

脚本内容如下:

#!/usr/bin/python3
# filename: dockerpullandpush.py
# 部署在hk小鸡里

import docker
import argparse
import subprocess
import json

def pull_and_push_image(image_name, docker_username, docker_password, harbor_url, harbor_username, harbor_password):
    # 从 image_name 中提取 harbor_project
    if '/' in image_name:
        harbor_project = image_name.split('/')[0]
        image_name_without_project = '/'.join(image_name.split('/')[1:])
    else:
        harbor_project = 'library'
        image_name_without_project = image_name

    # 创建 Docker 客户端
    client = docker.from_env()

    # 登录 Docker Hub
    client.login(username=docker_username, password=docker_password)

    # 拉取镜像
    print(f"Pulling image {image_name} from Docker Hub...")
    image = client.images.pull(image_name)

    # 标记镜像
    harbor_image_name = f"{harbor_url}/{harbor_project}/{image_name_without_project}"
    image.tag(harbor_image_name)

    # 登录 Harbor
    client.login(username=harbor_username, password=harbor_password, registry=harbor_url)

    # 检查项目是否存在
    project_exists_cmd = f"curl -s -u {harbor_username}:{harbor_password} -X GET https://{harbor_url}/api/v2.0/projects?name={harbor_project}"
    project_exists_output = subprocess.check_output(project_exists_cmd, shell=True)
    project_exists_data = json.loads(project_exists_output)

    if len(project_exists_data) == 0:
        # 如果项目不存在,则创建项目
        print(f"Project {harbor_project} does not exist in Harbor. Creating...")
        create_project_cmd = f"curl -s -u {harbor_username}:{harbor_password} -X POST -H \"Content-Type: application/json\" -d '{{\"project_name\": \"{harbor_project}\", \"public\": true}}' https://{harbor_url}/api/v2.0/projects"
        subprocess.check_output(create_project_cmd, shell=True)
        print(f"Project {harbor_project} created successfully.")

    # 推送镜像到 Harbor
    print(f"Pushing image {harbor_image_name} to Harbor...")
    client.images.push(harbor_image_name)

    print("Image pulled and pushed successfully.")

    # 清理下载的容器镜像
    print(f"Cleaning up downloaded image {image_name}...")
    client.images.remove(image_name)
    client.images.remove(harbor_image_name)

    print("Cleanup completed.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Pull images from Docker Hub and push them to Harbor.")
    parser.add_argument("image_names", nargs='+', help="Names of the images to pull and push")
    parser.add_argument("--docker-username", default="zzlyzq@126.com", help="Docker Hub username")
    parser.add_argument("--docker-password", default="xxx", help="Docker Hub password")
    parser.add_argument("--harbor-url", default="harbor.op123.ren:44301", help="Harbor URL")
    parser.add_argument("--harbor-username", default="admin", help="Harbor username")
    parser.add_argument("--harbor-password", default="xxx", help="Harbor password")

    args = parser.parse_args()

    for image_name in args.image_names:
        print(f"\nProcessing image: {image_name}")
        pull_and_push_image(image_name, args.docker_username, args.docker_password,
                            args.harbor_url, args.harbor_username, args.harbor_password)
最后修改:2024 年 08 月 16 日
如果觉得我的文章对你有用,请随意赞赏