删除同名的docker镜像

发布于 2025-01-17 08:25:13 字数 247 浏览 5 评论 0原文

服务器存储多个具有相同名称但具有不同标签的 docker 映像,我找到了一个很好的命令,它将搜索具有相同名称但具有不同标签的 docker 映像,并删除所有映像,仅保存最新的。 例子。

TestImage:v1
TestImage:v2
TestImage:v3
TestImage:v4

该命令将删除所有名为“TestImage”的图像,但保存 TestImage:v4(按日期最新)。

实际的命令。

Server storing multiple docker images with same name but with different tags, I am finding the good command which will search docker images with same name, but with different tags, and delete all, saving only the newest.
Example.

TestImage:v1
TestImage:v2
TestImage:v3
TestImage:v4

The command will delete all images which have name "TestImage" but save TestImage:v4 (which is newest by date).

The actual command .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2025-01-24 08:25:13

只要您安装了Docker CLI,以下内容应有效:

image_ids=$(docker image ls "$name" -q | tail -n +2 | xargs)
docker image rm "$image_ids"

第一个命令将所有本地图像带有给定名称。使用-Q标志意味着仅输出图像ID。
然后,tail程序跳过了这些ID中的第一个(换句话说,在第2行开始读取)。由于图像是从旧到旧的,这意味着要跳过最新的图像。
然后,XARGS将所有输出的ID带入同一行,以便Docker可以轻松解释它们。

在此步骤中获得的ID可能看起来像这样:

ae3c4906b72c 650d66a00131 18cac929dc4f

Docker Image RM将使用这些ID删除所有图像。

Provided you have the docker cli installed, the following should work:

image_ids=$(docker image ls "$name" -q | tail -n +2 | xargs)
docker image rm "$image_ids"

The first command loads all local images with the given name. Using the -q flag means that only the image ids are being outputted.
the tail program then skips the first one of these ids (in other words, starts reading at line 2). Since images are sorted from new to old, this means the newest image is skipped.
The xargs then brings all the outputted ids into the same line, so that docker can interpret them easily.

The ids obtained in that step may look something like this:

ae3c4906b72c 650d66a00131 18cac929dc4f

docker image rm will then remove all images with these ids.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文