Introduction
Managing storage in a Docker repository hosted on JFrog Artifactory can be challenging, especially when trying to identify and manage the largest Docker images. This is crucial for maintaining optimal storage usage, cost efficiency, and overall performance of your Artifactory instances.
Resolution
This Python script is designed to generate a storage report for a specified Docker repository in JFrog Artifactory. It fetches the list of Docker images and their associated tags, and retrieves storage information (size) for each tag. This helps in identifying the largest Docker images in the repository.
Prerequisites
- Python 3.6+ installed on your machine.
- Access to the Artifactory instance with sufficient permissions.
- Requests library installed (pip install requests).
The script performs the following tasks:
1. Initializes configuration variables for accessing the Artifactory instance.
2. Fetches the list of Docker images in the specified repository (using List Docker Repositories API)
3. For each Docker image, fetches the list of tags (using List Docker Tags API)
4. Retrieves storage information for each tag.
5. Writes the information to the report file.
How to use the script
1. Setting Configuration Variables:
Edit the configuration variables at the start of the script. Update the following with appropriate values:
- USERNAME: Your Artifactory username.
- ID_TOKEN: Your Artifactory password or token.
- ARTI_URL: Base URL of your Artifactory instance (e.g., https://artifactory.example.com).
- REPO: Name of the Docker repository (e.g., docker-local).
Example:
USERNAME = 'admin'
ID_TOKEN = 'my_secure_password_or_token'
ARTI_URL = 'https://artifactory.example.com'
REPO = 'docker-local'
2. Running the Script:
Save the script to a file ( such as fetch_docker_image_sizes.py) and run it using Python:
python3 fetch_docker_image_sizes.py
The script will generate a report file named <REPO>-tags-sizes.txt (for example: docker-local-tags-sizes.txt) in the same directory you ran the script.
The script for full reference:
#!/usr/bin/env python3
import sys, requests
# Configuration variables (edit directly below)
USERNAME = 'admin'
ID_TOKEN = 'password'
ARTI_URL = 'https://example.artifactory.com' # e.g. https://artifactory.example.com
REPO = 'docker-local'
REPORT_FILE = f'{REPO}-tags-sizes.txt'
ARTI_URL = ARTI_URL.rstrip('/')
auth = (USERNAME, ID_TOKEN)
open(REPORT_FILE, 'w').write(f"Storage Report for {REPO}\n=========================\n\n")
try:
r = requests.get(f"{ARTI_URL}/artifactory/api/docker/{REPO}/v2/_catalog", auth=auth, timeout=30)
r.raise_for_status()
data = r.json()
images = data.get('repositories') if isinstance(data, dict) else data
except Exception as e:
print('ERROR: failed to get catalog:', e, file=sys.stderr)
sys.exit(2)
for img in images or []:
img = str(img).strip()
if not img:
continue
try:
t = requests.get(f"{ARTI_URL}/artifactory/api/docker/{REPO}/v2/{img}/tags/list", auth=auth, timeout=30)
t.raise_for_status()
tags = t.json().get('tags', [])
except Exception:
tags = []
with open(REPORT_FILE, 'a') as fh:
fh.write('===\n')
fh.write(f"Tags for {img}:\n")
for tag in tags:
tag = str(tag)
fh.write(tag + '\n')
try:
c = requests.post(
f"{ARTI_URL}/artifactory/ui/artifactgeneral/artifactsCount",
auth=auth,
json={'name': REPO, 'repositoryPath': f"{REPO}/{img}/{tag}"},
timeout=30,
)
c.raise_for_status()
txt = c.text.strip()
if txt.startswith('{') and txt.endswith('}'):
txt = txt[1:-1].strip()
except Exception as e:
txt = f"ERROR: {e}"
fh.write(txt + '\n\n')
fh.write('===\n')
Output example:
Storage Report for docker-local
=========================
===
Tags for alpine:
202
"artifactsCount" : 3,
"artifactSize" : "3.22 MB"
3-first
"artifactsCount" : 3,
"artifactSize" : "3.22 MB"
latest
"artifactsCount" : 3,
"artifactSize" : "3.22 MB"
===
===
Tags for docker-buildx:
1.0
"artifactsCount" : 1,
"artifactSize" : "1.57 KB"
1.2.3
"artifactsCount" : 1,
"artifactSize" : "1.57 KB"
...
...
You can now utilize this script to identify and manage the largest Docker images in your Artifactory repository, helping you maintain efficient use of storage resources.