将本地镜像上传到私有harbor仓库
[TOC]
1.配置hosts文件
- 示例:域名
harbor.xxx.com
指向192.168.4.233
cat /etc/hosts | grep "harbor.xxx.com" > /dev/null || echo "192.168.4.233 harbor.xxx.com" >> /etc/hosts
cat /etc/hosts | grep harbor
2.通过api新建项目,比如 sqlfans
- 示例:地址
https://harbor.xxx.com:7443
,账号admin
,密码AdMin_147
docker login https://harbor.xxx.com:7443 -u admin -p AdMin_147
echo '{"project_name": "sqlfans", "metadata": {"public": "true"}, "storage_limit": -1}' > /tmp/project.json
curl -ku "admin:AdMin_147" -X POST -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/projects" -d @/tmp/project.json
3.通过api创建用户,比如 dev
- 示例:新用户
dev
,密码AdMin_258
echo '{"username":"dev", "email":"dev@qq.com", "password":"AdMin_258", "realname":"dev", "role_id":2}' > /tmp/user.json
curl -ku "admin:AdMin_147" -X POST -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/users" -d @/tmp/user.json
4.通过api增加项目的成员
- 示例:将 dev 添加到 sqlfans 项目的开发者(即role_id=2)
project_name=sqlfans
echo '{"role_id":2,"member_user":{"username":"dev"}}' > /tmp/role.json
curl -ku "admin:AdMin_147" -X POST -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/projects/${project_name}/members" -d @/tmp/role.json
5.测试 push 将本地镜像上传到私有harbor仓库
- 示例:使用普通用户(比如 dev)将本地镜像(比如 busybox:latest)上传到私有镜像仓库的指定项目(比如 sqlfans)
docker pull busybox:latest && docker images | grep busybox
docker login https://harbor.xxx.com:7443 -u dev -p AdMin_258
docker tag busybox:latest harbor.xxx.com:7443/sqlfans/busybox:20230412
docker push harbor.xxx.com:7443/sqlfans/busybox:20230412
docker images | grep busybox
6.测试 pull 拉取私有仓库的镜像到本地
- 示例:登录私有仓库(若项目设置公开访问,可不用登录),拉取镜像(比如 busybox:latest)到本地
#docker login https://harbor.xxx.com:7443 -u dev -p AdMin_258
docker pull harbor.xxx.com:7443/sqlfans/busybox:20230412
docker images | grep busybox
附:清除痕迹
curl -ku "admin:AdMin_147" -X DELETE -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/projects/sqlfans/repositories/busybox"
curl -ku "admin:AdMin_147" -X DELETE -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/projects/sqlfans"
#.获取user_id再根据user_id删除,比如dev用户的user_id为17
curl -sL http://iso.sqlfans.cn/linux/jq -o /usr/bin/jq && chmod +x /usr/bin/jq
curl -s -ku "admin:AdMin_147" -X GET -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/users/" | jq '.'
user_id=17
curl -ku "admin:AdMin_147" -X DELETE -H "Content-Type: application/json" "https://harbor.xxx.com:7443/api/v2.0/users/${user_id}"