Skip to content

Instantly share code, notes, and snippets.

@nishakm
Last active July 15, 2020 19:44
Show Gist options
  • Save nishakm/98d2fceaf8647bf521e98451123e5152 to your computer and use it in GitHub Desktop.
Save nishakm/98d2fceaf8647bf521e98451123e5152 to your computer and use it in GitHub Desktop.
Querying Docker API with python requests
>>> resp = requests.get("https://index.docker.io/v2/library/debian/manifests/bullseye")
>>> resp.headers
{'Content-Type': 'application/json', 'Docker-Distribution-Api-Version': 'registry/2.0', 'Www-Authenticate': 'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/debian:pull"', 'Date': 'Mon, 13 Jul 2020 17:13:00 GMT', 'Content-Length': '157', 'Strict-Transport-Security': 'max-age=31536000'}
>>> token = requests.get("https://auth.docker.io/token", params={"service": "registry.docker.io", "scope":"repository:library/debian:pull"})
>>> token
<Response [200]>
>>> token.json()
{'token': 'eyJhbGciOiJS...
>>> manifest = requests.get("https://index.docker.io/v2/library/debian/manifests/bullseye", headers={'Authorization': 'Bearer ' + token.json()['token'], Accept='application/vnd.docker.distribution.manifest.v2+json'})
>>> manifest.text
'{\n "schemaVersion": 2,\n "mediaType": "application/vnd.docker.distribution.manifest.v2+json",\n "config": {\n "mediaType": "application/vnd.docker.container.image.v1+json",\n "size": 1499,\n "digest": "sha256:546a93a8e0a65e685836aa394497b98a5f694557b0c060f1f5cca472afe4b6c2"\n },\n "layers": [\n {\n "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",\n "size": 51413507,\n "digest": "sha256:07f1101e3e85b531862163b4177bce7f401eac107a6537ab74195b395aab30b7"\n }\n ]\n}'
>>> hashlib.sha256(resp_manifest.text.encode('utf-8')).hexdigest()
'e8bab439d86e39a0f694344ab0c11f77bce1460c13c903c437ee9da69c7b6fa3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment