欢迎访问陈同学博客原文

现许多团队使用 Nexus 来管理 Docker 镜像,产品不断迭代,镜像仓库占用的磁盘空间也越来越大。由于 Nexus 的控制台并未提供批量操作镜像功能,清理镜像十分不便。本文分享一个清理 Nexus 中 Docker 镜像的小工具 — nexus-cli

可参考 nexus-cli 的相关资料,nexus-cli GitHub开发者博客原文Nexus 官方博客文章

下面结合 nexus-cli + sublime 做一个演示。

下载脚本并授权

wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli
chmod +x nexus-cli

配置待清理的库

$ nexus-cli configure
Enter Nexus Host: http://127.0.0.1:8081
Enter Nexus Repository Name: demo
Enter Nexus Username: admin
Enter Nexus Password: xxxxxx

http://127.0.0.1:8081 是 Nexus 服务的地址,为了安全,可以直接在 Nexus 所在的机器处理。

列出所有镜像

$ nexus-cli image ls
oauth-service
report-service
fund-service
...

这里仅列出三个作为例子。

清理镜像

nexus-cli image delete -name IMAGE_NAME -keep X,-keep X 表示保留几个tag

清理单个镜像的tag,这里仅保留最新的两个tag

$ nexus-cli image delete -name fund-service -keep 2
fund-service:2018.11.1-113322 image will be deleted ...
fund-service:2018.11.1-113322 has been successful deleted
fund-service:2018.11.1-145912 image will be deleted ...
fund-service:2018.11.1-145912 has been successful deleted
...

清理所有镜像的tag,结合 sublime 的批量处理功能,十分方便。

一键清理 Nexus 中无用的 Docker 镜像

清理磁盘空间

最后,创建两个Nexus Task 来清理物理空间。先运行 Purge unused docker manifests and images,再运行 Compact blob store

一键清理 Nexus 中无用的 Docker 镜像

此时,可以查看磁盘目录 **sonatype-work/nexus3/blobs/ ** 的磁盘占用情况。

$ du -lh --max-depth=1 . | grep demo
2.1G	./demo

小结

由于开发测试环境中应用迭代非常频繁,镜像消耗磁盘的速度也非常快。若磁盘空间足够,每隔一定时间手工清理即可;若需要频繁清理,使用脚本自动化处理也十分方便。

文首列举的 nexus-cli 的资料中,有 nexus-cli 的更多操作命令,可自行参考。


欢迎关注陈同学的公众号,一起学习,一起成长

一键清理 Nexus 中无用的 Docker 镜像

相关文章:

  • 2021-08-12
  • 2023-01-06
  • 2021-12-13
  • 2021-12-11
  • 2022-12-23
  • 2021-07-08
  • 2021-12-15
猜你喜欢
  • 2022-02-11
  • 2022-02-08
  • 2022-01-01
  • 2021-11-23
相关资源
相似解决方案