博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Docker Remove Image:如何删除Docker镜像示例
阅读量:2529 次
发布时间:2019-05-11

本文共 7865 字,大约阅读时间需要 26 分钟。

We live in an era where storage is becoming cheaper everyday. We can just send everything to the cloud and pay almost nothing.

我们生活的时代每天都在变得越来越便宜。 我们可以将所有内容发送到云中,而几乎不支付任何费用。

So why would we need to worry about deleting Docker images?

那么,为什么我们要担心删除Docker映像呢?

First of all, there are still some mission critical workloads that can't be moved to the cloud, especially those in heavily regulated industries like law or healthcare.But to better answer that question, I would say that we as developers often find ourselves out of space on our local machines.

首先,仍然有一些关键任务工作负载无法转移到云中,特别是在法律或医疗保健等受严格监管的行业中。但是为了更好地回答这个问题,我想说我们作为开发人员经常会发现自己我们本地机器上的空间。

Let's do a quick analysis of this to explore that further:

让我们对这个进行快速分析,以进一步探索它:

SELECT tag,       title,       answer_count,       favorite_count,       score,       view_count VIEWSFROM  (SELECT title,          answer_count,          favorite_count,          view_count,          score,          SPLIT(tags, '|') tags   FROM `bigquery-public-data.stackoverflow.posts_questions`          posts_questions), UNNEST(tags) tagWHERE tag = 'docker'  AND title LIKE '%space left%'ORDER BY VIEWS DESC

Query Results:

查询结果:

So it does't happen just with me, right? Look at how many views we have on those StackOverflow posts. If you are wondering, the number is 465687 views for posts matching the search query.

所以这不仅仅发生在我身上,对吗? 看看我们对那些StackOverflow帖子有多少意见。 如果您想知道,与搜索查询匹配的帖子的查看次数为465687

Luckily for us, today we are going to see some easy-to-use examples on how to delete our dangling and unused docker images to help ourselves out.

对我们来说幸运的是,今天我们将看到一些易于使用的示例,这些示例说明了如何删除悬空和未使用的docker映像以帮助自己。

什么是悬空的和未使用的Docker映像? (What are dangling and unused Docker images?)

drawing

What is the difference between dangling and unused images, you might ask?

您可能会问,悬空的图像和未使用的图像有什么区别?

A dangling image means that you've created a new build of the image but haven't given it a new name. Think about those old, forgotten images that no one knows what to do with anymore – those are "dangling images".

悬空的图像表示您已经创建了图像的新版本,但未为其赋予新名称。 想一想那些旧的,被遗忘的图像,这些图像现在再也没人知道该怎么做了,它们是“悬而未决的图像”。

They are left untagged and display <none> on their name when you run docker images.

当您运行docker images时,它们将保持未标记状态并在其名称上显示<none>

On the other hand, an unused image means that it has not been assigned or is not being used in a container.

另一方面,未使用的图像表示尚未分配或未在容器中使用它。

For example, when running docker ps -a – it will list all your currently running containers plus exited containers. Any images being used inside any of containers are shown as "used images", and any others are unused.

例如,在运行docker ps -a –它会列出您当前正在运行的所有容器以及已退出的容器。 在任何容器内使用的任何图像均显示为“已使用图像”,而其他任何图像均未使用。

删除Docker映像 (Delete Docker Images)

Now let's see some examples of how to delete Docker images.

现在,让我们看一些如何删除Docker映像的示例。

我们的案例研究 (Our case study)

drawing

The Busy Cat Corp is a fictional company that captures cat behavior data, and provides recommendations to cat owners on how to make their pets busier and happier.All their workloads are containerized, and they use the following database images:, , and .

The Busy Cat Corp是一家虚构的公司,可以捕获猫的行为数据,并向猫主人提供如何使他们的宠物更忙,更快乐的建议。所有工作量都被容器化,并且使用以下数据库图像: , , 和

Their developers are constantly running out of space on their machines, and they are top users of StackOverflow – aren't we all?So they asked us for some quick examples of how to delete some images and get their space back.

他们的开发人员一直在计算机上空间不足,他们是StackOverflow的主要用户–不是所有人吗?所以他们要求我们提供一些有关如何删除某些图像并重新获得空间的快速示例。

First let's take a look at the machine of one of their developers.

首先,让我们看一下其中一位开发人员的机器。

docker images

Output

输出量

REPOSITORY  TAG          IMAGE ID            CREATED              SIZE
9c872a6119cc About a minute ago 384MBmysql latest 5ac22cccc3ae 43 hours ago 544MBcassandra 3 9fab0c92a93d 4 days ago 384MBadoptopenjdk 8-jre... 2bf0172ac69b 4 days ago 210MBmongo latest 6d11486a97a7 2 weeks ago 388MBpostgres latest b97bae343e06 6 weeks ago 313MB

That's cool, they have all the images from their workloads downloaded. But look at the disk space – it's more than 2GB! Let's see what we can do for them.

太酷了,他们已经下载了工作负载中的所有图像。 但是看看磁盘空间–它超过2GB ! 让我们看看我们能为他们做些什么。

删除Docker悬空图像 (Delete Docker dangling images)

We'll start by looking for dangling images.

我们将从寻找晃晃的图像开始。

docker images -qf "dangling=true"

Output

输出量

REPOSITORY  TAG          IMAGE ID            CREATED              SIZE
9c872a6119cc About a minute ago 384MB

We have one, so we are going to clear it up.

我们有一个,所以我们要清理它。

Delete the dangling image

删除悬空的图像

docker rmi $(docker images -qf "dangling=true")
drawing

删除Docker未使用的映像 (Delete Docker unused images)

Next we are looking for unused images.

接下来,我们正在寻找未使用的图像。

docker ps -a

Output

输出量

CONTAINER ID  IMAGE   CREATED           NAMESb6387b343b81  mysql   16 minutes ago    some-mysql

We only have one container running the  mysql  image, so all the other images are unused.

我们只有一个容器运行mysql映像,因此所有其他映像均未使用。

So that we don't have to do it manually, we can put together a script that shows all of the unused images to verify them.

为了不必手动进行操作,我们可以将显示所有未使用图像的脚本放在一起以进行验证。

Output

输出量

adoptopenjdk:8-jre-hotspot-bionic is not being used.cassandra:3 is not being used.mongo:latest is not being used.postgres:latest is not being used.

Then it deletes the unused images.

然后删除未使用的图像。

After deleting both dangling and unused images we can look at what we have left.

删除悬空和未使用的图像后,我们可以查看剩下的内容。

docker images

Output

输出量

REPOSITORY  TAG          IMAGE ID            CREATED              SIZEmysql        latest      5ac22cccc3ae        43 hours ago         544MB

So we only have the mysql  image remaining, that's great!

所以我们只剩下mysql映像了,太好了!

drawing

使用prune删除所有过时的Docker映像 (Delete all obsolete Docker images with prune)

Those commands sound great, but a second developer said they didn't care about the differences between dangling and unused images.

这些命令听起来不错,但另一位开发人员表示,他们并不关心悬空图像和未使用图像之间的差异。

All they wanted was to clear obsolete images and get their disk space back.

他们只想清除过时的映像并收回磁盘空间。

Personally this is what I usually do.
就我个人而言,这通常是我要做的。

So we can just use Docker's prune commands.

因此,我们只能使用Docker的prune命令。

# First delete all stopped containersdocker container prune# Then delete both dangling and unused imagesdocker image prune --all

This will delete both unused and dangling images. Or in other words images without at least one container associated with them.

这将删除未使用的图像和悬挂的图像。 换句话说,就是没有至少一个容器的图像。

Note: this is why we needed to first delete the stopped containers in the code above.

注意:这就是为什么我们需要首先删除上面代码中的已停止容器。

结束语 (Wrapping up)

In this article we saw how to delete Docker Images, and we used a fictional company to explain it with some easy-to-use examples.

在本文中,我们了解了如何删除Docker映像,并使用了一家虚构的公司通过一些易于使用的示例对其进行了解释。

It's important to point out that you shouldn't use Docker to keep a history of your old images. For a developer environment that's fine, and you can even automate the image clean up workload if you have to deal with a lot of them.

重要的是要指出您不应该使用Docker来保存旧映像的历史记录。 对于很好的开发人员环境,如果必须处理大量图像,甚至可以自动化图像清理工作量。

But for a production workload, you should be using a Container Registry solution to handle your Docker images.

但是对于生产工作负载,您应该使用Container Registry解决方案来处理Docker映像。

There are many Container Registry solutions out there, like Google Cloud Platform with and Docker Enterprise with . And if you are in the open source world, you can just use :).Thanks for reading!

那里有许多Container Registry解决方案,例如带有 Google Cloud Platform和带有 Docker Enterprise。 如果您在开源世界中,则可以使用 :)。谢谢阅读!

* Illustrations by Icons 8 from

*来自的Icons 8的

If you found this helpful, or wish to challenge or extend anything raised here, feel free to contact me on or . Let's connect!

如果您认为这有帮助,或者希望挑战或扩展此处提出的任何内容,请随时通过或与我联系。 让我们连接!

翻译自:

转载地址:http://lmgwd.baihongyu.com/

你可能感兴趣的文章
HDU 1300 Pearls (DP)
查看>>
2014年军训总结
查看>>
扩展 -------jQuery
查看>>
Winform跨线程操作最简单的办法
查看>>
[51nod1532]带可选字符的多字符串匹配
查看>>
socket 基于udp实现远程执行命令
查看>>
读取本地json文件,解析json
查看>>
【学习】循环语句1027
查看>>
Git提交代码报错Git push error:src refspec XXX matches more than one解决方案
查看>>
软件设计规格说明书
查看>>
bzoj 1500: [NOI2005]维修数列 -- splay
查看>>
设计模式 - 简单工厂
查看>>
数组与指针杂记
查看>>
四色原理
查看>>
Codeforces Round#500 Div.2 翻车记
查看>>
再更新ww的mingw MinGW-full-20101119
查看>>
Benefit UVA - 11889
查看>>
全排列 最详细的解题报告
查看>>
c++ web服务器
查看>>
android机型排行榜(201509)
查看>>