@
Niphor 这个脚本我命名为
dockerpullandpush.py ,部署在 hk 的小鸡上,通过输入参数镜像名称,实现从 dockerhub 拉取,并 push 到国内的 harbor 站点。如果 harbor 站点没有对应的 repository ,也会自动创建。以下脚本修改了 dockerhubu 和自建 harbor 的密码,改成自己的就可以用了。
#!/usr/bin/python3
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="
[email protected]", 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)