Last active
July 19, 2022 12:56
-
-
Save gintsmurans/dc7d3457aa68e966805cdb95556ac26a to your computer and use it in GitHub Desktop.
Cleanup docker registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This script will cleanup docker registry images, that are older than 7 days (see REMOVE_OLDER_THAN_DAYS). | |
# Note: This will only work on v2 registry. | |
# Before running make sure you have: | |
# 1. Installed requirements by running `python3 -m pip install -r requirements.txt` | |
# 2. Put registry password in `.registry-password` file | |
# 3. Updated correct values in `Constants` section | |
# Then run `python3 cleanup.py` or `chmod +x cleanup.py && ./cleanup.py` | |
# After cleaning registry, run garbage collector on registry, for example: `docker compose exec registry sh -c "/bin/registry garbage-collect --delete-untagged /etc/docker/registry/config.yml"` | |
import re | |
import sys | |
import json | |
import logging | |
from datetime import datetime, timezone | |
from docker_registry_client import DockerRegistryClient | |
# Constants | |
DOCKER_REGISTRY_USERNAME = 'admin' | |
DOCKER_REGISTRY_PASSWORD_FILE = "./.registry-password" | |
DOCKER_REGISTRY_ADDRESS = "https://localhost:5000" | |
REMOVE_OLDER_THAN_DAYS = 7 | |
# Logging | |
logger = logging.getLogger("Docker cleanup") | |
logger.setLevel(logging.DEBUG) | |
handler = logging.StreamHandler(sys.stdout) | |
handler.setLevel(logging.DEBUG) | |
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
# Setup client | |
password = None | |
with open(DOCKER_REGISTRY_PASSWORD_FILE) as f: | |
password = f.read() | |
password = password.strip() | |
logger.debug(f"Connecting to registry {DOCKER_REGISTRY_ADDRESS}") | |
client = DockerRegistryClient( | |
DOCKER_REGISTRY_ADDRESS, | |
verify_ssl=False, | |
username=DOCKER_REGISTRY_USERNAME, | |
password=password | |
) | |
logger.debug(f"Registry version: {client.api_version}") | |
# List all repositories | |
repositories = client.repositories() | |
logger.debug(f"Found: {len(repositories)} repositories") | |
dateRegex = re.compile(r"\.[0-9]+") | |
now = datetime.now(timezone.utc) | |
for repository in repositories: | |
logger.debug(f"Gathering tags for repository {repository}") | |
repo = client.repository(repository) | |
tags = repo.tags() | |
for tag in tags: | |
data, digest = repo.manifest(tag) | |
try: | |
historyData = data["history"][0]["v1Compatibility"] | |
except KeyError: | |
logger.error(f"No history data for tag {tag}") | |
continue | |
try: | |
historyData = json.loads(historyData) | |
except json.JSONDecodeError: | |
logger.error(f"Could not decode history data for tag {tag}") | |
continue | |
# Check if the image is older than a week | |
# We are throwing away microseconds, because python do not recognize them and we don't need them | |
created = dateRegex.sub("", historyData["created"]) | |
dt = datetime.strptime(created, "%Y-%m-%dT%H:%M:%S%z") | |
dateDiff = now - dt | |
if dateDiff.days > REMOVE_OLDER_THAN_DAYS: | |
logger.info(f"Removing tag {tag}. {dateDiff.days} days old.") | |
repo.delete_manifest(digest) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker-registry-client |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment