简单的Docker注册表清洁器用Python编写的使用Docker注册表HTTP API V2。
Docker注册表清洁器是一种命令行应用程序,旨在自动从Docker注册表中清洁旧的Docker图像。它提供了一种基于各种标准来定义清理作业的灵活和可配置的方法。
可以使用Python 3.11或更高的码头注册表清洁器安装和运行。请按照以下步骤安装并运行应用程序:
克隆存储库或从GitHub存储库下载源代码。
通过运行以下命令安装所需的依赖项或简单地使用Pipenv工具:
pip install -r requirements.txt
包括开发的所有依赖关系
pip install -r requirements.all.txt
Docker注册表清洁器需要3个配置文件: config/config.yaml , config/jobs.yaml和config/manual.yaml 。
示例可以在存储库中找到
Exmaple:
# Your docker registry url.
registry_url : https://your.registry.com/
# Environment variables works only for username, password and proxy
# Format: <field>: "__ENV: <YOUR_ENV_NAME>"
# or you can use strings
# username: your_username
username : " __ENV: ENV_VAR_NAME "
password : " __ENV: ENV_VAR_NAME2 "
# Optional; Format: <scheme>://[username:password@]<address>[:port]
proxy : " __ENV: PROXY "
# Optional, default 20, max 120, min 1
timeout : 20
# Optional; default 20
max_concurrent_requests : 20registry_url :要清洁的Docker注册表的URL。username :用于Docker注册表的身份验证的用户名。password :使用Docker注册表进行身份验证的密码。max_concurrent_requests :该应用程序可以向注册表提出的最大并发请求数。默认值:20。proxy :代理服务器的URL用于向注册表提出请求。如果不需要,请留空或删除。timeout :每个HTTP请求的超时秒数。默认值:20。username , registry和proxy ,如示例所示在jobs.yaml中定义的清理工作列表。每个作业都由以下参数定义:
- name : clean-dev-tags
# Optional
description : Clean dev tags every 24 hours
# List of repositories to clean
# You can use regular expression to match multiple repositories
# Format: r/regexp/
repositories :
- r/^base-w+$/
- scheduler-dev
- admin-panel-dev
# Pythonic regular expressions to match the tag name
# Be careful using this. Check your regexp at https://regex101.com/
tag_regexps :
- v5.d+.d+-dev$
- develop-[dw]+$
# Do not delete the last n tags, even
# if they are older than the specified number of days
save_last : 5
# Perform checks and do cleanup every x hours
clean_every_n_hours : 24
# Delete tags if their creation date is older than y days
older_than_days : 5name :工作的名称。description :作业的可选描述。repositories :存储库名称regexps的字符串数组和名称以清除图像。tag_regexps :正则表达式的字符串数组匹配存储库中的标签。只有与这些正则表达式匹配的标签要进行清理。使用此选项时要小心。在https://regex101.com/上查看您的绩效save_last :每个存储库中要保存的最后一个标签的数量(排除在清理中)。clean_every_n_hours :连续清理之间的小时间隔用于此作业。older_than_days :将考虑将标签考虑清理的日子的年龄。 标签通过正则表达式进行分组,每个正则表达式代表不同的组。如果您需要独立保存release-xx-dev和release-xx的最后5个标签,但是您的正则表达式可以匹配两种类型的标签,则最终将获得一个混合阵列,其中包含5个release-xx-dev和release-xx标签。为了防止这种情况,您需要添加两个不同的正则表达式以独立匹配release-xx和release-xx-dev 。
与jobs.yaml的区别:
--jobs选项一起使用,该选项允许您manual.yaml中声明的作业。使用以下命令运行Docker注册表清洁器:
python main.py [OPTIONS]
$ python -h
usage: main.py [-h] [--debug] [--watch] [--jobs JOBS [JOBS ...]] [--http-logs]
Automatic cleaner of old docker images
options:
-h, --help show this help message and exit
--debug The application will generate logs without actually deleting the images
--watch Endless operation of the application for auto cleanup. Will be used 'config.yaml'
--jobs JOBS [JOBS ...]
List of jobs in `manual.yaml` to run. Example: --jobs clean-dev-tags clean-prod-older-15
--http-logs Enable http logs for every request
Docker注册表清洁工接受以下选项:
--debug :如果提供的话,该应用程序将生成日志,而无需实际删除图像。如果您需要检查图像,请使用此选项该应用程序将删除--watch :如果提供,该应用程序将以无尽的循环运行,并根据config.yaml中的配置定期执行清理作业。--jobs :您要运行的手册中定义的作业名称列表。例如: --jobs clean-dev-tags clean-prod-older-15 。--http-logs :如果提供,将为应用程序提出的每个请求启用HTTP日志。如果您一直在使用--debug选项使用该应用程序,并且需要立即触发清理过程,则可能需要清理latest_cleanup.json文件或删除包含您感兴趣的作业结果的特定部分。执行此步骤将允许您立即触发下一个清理,而无需尝试下一个自动清理周期。
在以--debug模式运行应用程序后,您必须使用Ctrl + C停止应用程序,然后在没有--debug选项的情况下重新启动它以恢复正常的清理过程。
logs/cleaner.log中的应用程序日志cache/history.log中使用时间戳执行cache/latest_cleanup.json 该存储库包含基于Alpine Linux和Dockerfile.bullseye基于Debian 11的根目录中的两个Dockerfiles: Dockerfile 。
两个Docker容器均设计为运行相同的命令: python3 main.py --watch --http-logs ,它以无限循环开始清理过程。一旦开始,容器将继续无限期地运行,而无需任何进一步的干预。此设置可确保用于Docker图像的平滑而连续的清理过程。
Alpine:
docker build -t cleaner:alpine .
Debian:
docker build -t cleaner:debian -f Dockerfile.bullseye .
要运行Docker注册表清洁器,您需要提供必要的环境变量以进行身份验证,并在必要时将新鲜的配置文件安装在容器中:
docker run
-v ./config/config.yaml:/app/config/config.yaml
-v ./config/jobs.yaml:/app/config/jobs.yaml
cleaner
停止:
docker container stop <container_name> -s KILL