缓存失效后如何删除缓存/中间 docker 图像 [英] How to delete cached/intermediate docker images after the cache gets invalidated

查看:21
本文介绍了缓存失效后如何删除缓存/中间 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 镜像由几个中间层组成,这些中间层逐渐变得非常大.每次运行的大多数中间图像都是相同的,因此 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. 摆脱所有过时的容器,这些容器是根据过时的图像创建的

  • 使用命令 docker ps -a
  • 显示所有容器
  • 如果仍在运行,请使用命令 docker stop [containerID]
  • 停止过时的容器
  • 使用 command docker rm [containerID]
  • 删除它们

    1. 使用命令删除过时的图像: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.

    编辑 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.

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

    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.

    编辑 2

    图像清除命令已更改.现在正确的命令是:

    The command for image purging has changed. Right now the proper command is:

    docker image prune
    

    这是一个带有文档的链接

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

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