Created
September 9, 2017 17:34
-
-
Save jseabold/f4f29477a391a903b1da00bfee9f9350 to your computer and use it in GitHub Desktop.
Some functions for dealing with docker registry manifests
This file contains 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
import urllib | |
import docker | |
def get_manifest_auth_token(repo): | |
# https://docs.docker.com/registry/spec/auth/token/ | |
query = urllib.parse.urlencode({ | |
'service': 'registry.docker.io', | |
'scope': 'repository:{repo}:pull'.format(repo=repo) | |
}) | |
login = urllib.parse.urlunsplit(( | |
'https', | |
'auth.docker.io', | |
'token', | |
query, | |
'' | |
)) | |
token = requests.get(login, json=True).json()["token"] | |
return token | |
def get_tags(repo): | |
token = get_manifest_auth_token(repo) | |
url = "https://registry.hub.docker.com/v2/{repo}/tags/list" | |
tags = requests.get( | |
url.format(repo=repo), | |
headers={ | |
'Authorization': 'Bearer {}'.format(token) | |
}, | |
json=True | |
).json()['tags'] | |
return tags | |
def get_manifest(repo, tag): | |
# https://docs.docker.com/registry/spec/api/#manifest | |
# https://docs.docker.com/registry/spec/manifest-v2-2/ | |
token = get_manifest_auth_token(repo) | |
url = "https://registry.hub.docker.com/v2/{repo}/manifests/{tag}" | |
response = requests.get( | |
url.format(repo=repo, tag=tag), | |
headers={ | |
'Authorization': 'Bearer {}'.format(token), | |
'Accept': 'application/vnd.docker.distribution.manifest.v2+json' | |
}, | |
json=True | |
) | |
return response.json() | |
def get_tag_digest(repo, tag): | |
manifest = get_manifest(repo, tag) | |
return manifest['config']['digest'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment