缓存无效后如何删除已缓存/中间的Docker映像 [英] How to delete cached/intermediate docker images after the cache gets invalidated

查看:62
本文介绍了缓存无效后如何删除已缓存/中间的Docker映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CI管道,可为每次运行的管道为我的应用程序构建一个docker映像(并且该管道是通过将代码推送到git存储库触发的.)

I have a CI-pipeline that builds a docker image for my app for every run of the pipeline (and the pipeline is triggered by a code-push to the git repository.)

泊坞窗映像由几个中间层组成,这些中间层的大小逐渐变大.每次运行时,大多数中间映像都是相同的,因此大大利用了docker的缓存机制.

The docker image consists of several intermediate layers which progressively become very large in size. Most of the intermediate images are identical for each run, hence the caching mechanism of docker is significantly utilized.

但是,问题在于每次运行的最后几层是不同的,这是因为它们是由dockerfile中的COPY语句导致的,其中将已构建的应用程序工件复制到映像中.由于工件在每次运行时都会被修改,因此已经缓存的最底端图像将始终无效.这些图像的大小均为800mb.

However, the problem is that the final couple layers are different for each run, as they result from a COPY statement in dockerfile, where the built application artifacts are copied into the image. Since the artifacts are modified for every run, the already cached bottommost images will ALWAYS be invalidated. These images have a size of 800mb each.

我可以使用什么docker命令来识别(并删除)这些被新映像替换的映像,即它们何时失效?

What docker command can I use to identify (and delete) these image that gets replaced by newer images, i.e. when they get invalidated?

我想让我的CI管道在运行结束时删除它们,以免它们最终挂在CI服务器上而浪费大量磁盘空间.

I would like to have my CI-pipeline to remove them at the end of the run so they don't end up dangling on the CI-server and waste a lot of disk space.

推荐答案

如果我正确理解:每执行一次代码推送,CI管道就会创建新映像,并在其中部署新版本的应用程序.结果,以前创建的图像已过时,因此您要删除它.为此,您必须:

If I understand correctly: With every code push, CI pipeline creates new image, where new version of application is deployed. As a result, previously created image becomes outdated, so you want to remove it. To do so, you have to:

  1. 摆脱掉所有过时的容器,这些容器是根据过时的图像创建的

  1. Get rid of all outdated containers, which where created from outdated image

  • 使用命令 docker ps -a
  • 显示所有容器
  • 如果仍在运行,请使用命令 docker stop [containerID]
  • 停止过时的容器
  • 使用命令docker rm [containerID]
  • 删除它们
  • display all containers with command docker ps -a
  • if still running, stop outdated containers with command docker stop [containerID]
  • remove them with command docker rm [containerID]

使用以下命令删除过时的图像: docker rmi [imageID]

Remove outdated images with command: docker rmi [imageID]

总结为什么需要此过程:您不能删除任何图像,直到任何现有容器使用它为止(即使停止的容器仍然需要其图像).因此,您应该先停止并删除旧容器,然后再删除旧图像.

To sum up why this process is needed: you cannot remove any image, until it is used by any existing container (even stopped containers still require their images). For this reason, you should first stop and remove old containers, and then remove old images.

检测部分和删除过程的自动化应基于CI版本在创建新图像时生成的图像版本和容器名称.在不知道您的管道如何正常工作的情况下,很难提供任何具体的解决方案.

Detection part, and automation of deletion process should be based on image versions and container names, which CI pipeline generates while creating new images. It's hard to offer any concrete solution without knowing how your pipeline exactly works.

编辑1

要列出所有与任何标记图像都没有关系的图像,可以使用命令: docker images -f dangling = true .您可以使用以下命令将其删除: docker images purge .

To list all images, which have no relationship to any tagged images, you can use command: docker images -f dangling=true. You can delete them with the command: docker images purge.

这里只需要记住一件事:如果您在未标记图像的情况下构建图像,该图像将出现在悬空"图像列表中.您可以通过在构建标签时提供标签来避免这种情况.

Just one thing to remember here: If you build an image without tagging it, the image will appear on the list of "dangling" images. You can avoid this situation by providing a tag when you build it.

这篇关于缓存无效后如何删除已缓存/中间的Docker映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆