nexus 清理 docker 镜像
下载配置 nexus-cli
看网上文档都用如下地址,但现在已经不能下载:
wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli
chmod +x nexus-cli
在 github 上下载:
wget https://github.com/heyonggs/nexus-cli/releases/download/0.0.3/nexus-cli-linux
mv nexus-cli-linux /usr/local/bin/nexus-cli
chmod +x /usr/local/bin/nexus-cli
查看帮助命令
[root@nexus ~]# nexus-cli --help
NAME:
Nexus CLI - Manage Docker Private Registry on Nexus
USAGE:
nexus-cli [global options] command [command options] [arguments...]
VERSION:
0.0.2
AUTHOR:
Eugen Mayer, Karol Buchta, Mohamed Labouardy <->
COMMANDS:
configure Configure Nexus Credentials
image Manage Docker Images
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
配置凭证
[root@nexus ~]# nexus-cli configure
Enter Nexus Host: https://nexus.***.com
Enter Nexus Repository Name: docker-public
Enter Nexus Username: admin
Enter Nexus Password: Removed potential trailing slash on Nexus Host URL, now: https://nexus.***.com
Configuration saved to succesfully to: /root/.nexus-cli
列出所有镜像
[root@nexus ~]# nexus-cli image ls
admin/admin
ai/chrome
ai/rockylinux-9-desktop
ai/ubuntu-desktop
......
列出特定镜像的所有 tag
[root@nexus ~]# nexus-cli image tags -name tools/nginx
Error parsing version2: "No Major.Minor.Patch elements found"
1.16
1.20.1
There are 2 images for tools/nginx
查看特定 tag 的信息
[root@nexus ~]# nexus-cli image info -name tools/nginx -tag 1.16
Image: tools/nginx:1.16
Size: 6645
Layers:
sha256:54fec2fa59d0a0de9cd2dec9850b36c43de451f1fd1c0a5bf8f1cf26a61a5da4 27098254
sha256:5546cfc927724eff7f5a134801904dee21064bd49912a7c4daa550175939cbce 23879166
sha256:50f62e3cdaf71a6b4ba0cbe2115ce42d90cce8301cbf8220d2a99d19e0b85690 202
删除特定 tag 镜像
[root@nexus ~]# nexus-cli image delete -name tools/nginx -tag 1.16
# 删除多个
nexus-cli image delete -name dockernamespace/yourimage -tag 1.16,1.20
在删除之前运行测试
nexus-cli image delete -name tools/nginx -keep 4 -dry-run
删除所有标签,但保留最近的4。注意,latest也算,被认为是“最近的”。
nexus-cli image delete -name dockernamespace/yourimage -keep 4
清理磁盘空间
创建两个Nexus Task 来清理物理空间。先运行 Purge unused docker manifests and images,再运行 Compact blob store,也可以直接手动执行清理。
如果没有这两个任务的可以直接创建
Compact blob store
Purge unused docker manifests and images
使用shell脚本每天定时清理一次
我的镜像tag格式:2024-07-01-15-04-22,大家可以根据自己的镜像tag修改脚本
#!/bin/bash
# 获取所有镜像列表
images=$(nexus-cli image ls)
# 遍历每个镜像
for image in $images; do
# 获取镜像的tag列表
tags=$(nexus-cli image tags --name "$image")
# 遍历每个tag
for tag in $tags; do
# 判断tag是否以 年-月-日-时 开头
if [[ $tag =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2} ]]; then
echo "删除镜像: $image:$tag"
nexus-cli image delete --name "$image" --tag "$tag"
fi
done
done
echo "删除完成."
添加执行权限
chmod +x del_nexus_docker.sh
添加定时任务,每天凌晨1点30执行一次
crontab -e
30 01 * * * del_nexus_docker.sh > /dev/null 2>&1 &
记得也要配置nexus task定时清理任务。