Skip to content

Instantly share code, notes, and snippets.

@rightson
Last active June 17, 2023 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightson/5a966eae6d40ee0b36dad9fa329d2c0f to your computer and use it in GitHub Desktop.
Save rightson/5a966eae6d40ee0b36dad9fa329d2c0f to your computer and use it in GitHub Desktop.
docker pull/load from CLI
import tarfile
import subprocess
import json
import sys
image = sys.argv[1]
# Create a dictionary for the image manifest
manifest_dict = {
'schemaVersion': 2,
'mediaType': 'application/vnd.docker.distribution.manifest.v2+json',
'config': {
'mediaType': 'application/vnd.docker.container.image.v1+json',
'size': os.path.getsize(f'{image}/config.json')
},
'layers': []
}
# Add each layer to the manifest dictionary
for i, layer in enumerate(os.listdir(image)):
if layer.endswith('.tar'):
manifest_dict['layers'].append({
'mediaType': 'application/vnd.docker.image.rootfs.diff.tar.gzip',
'size': os.path.getsize(f'{image}/{layer}')
})
# Save the manifest dictionary as manifest.json
with open(f'{image}/manifest.json', 'w') as manifest_file:
json.dump(manifest_dict, manifest_file)
# Create a tar archive with the image layers and configuration
with tarfile.open(f'{image}.tar', 'w') as tar:
tar.add(f'{image}/manifest.json', arcname='manifest.json')
tar.add(f'{image}/config.json', arcname='config.json')
for i in os.listdir(image):
if i.endswith('.tar'):
tar.add(f'{image}/{i}', arcname=i)
# Load the tar archive into Docker
result = subprocess.run(['docker', 'load', '-i', f'{image}.tar'], capture_output=True, text=True)
# Print the result of the Docker load command
print(result.stdout)
import requests
import os
def get_token(image, tag):
response = requests.get(f'https://auth.docker.io/token?service=registry.docker.io&scope=repository:{image}:pull')
response_json = response.json()
return response_json['token']
# Usage:
image = 'library/postgres'
tag = 'latest'
token = get_token(image, tag)
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.docker.distribution.manifest.v2+json',
}
manifest_req = requests.get(f'https://registry-1.docker.io/v2/{image}/manifests/{tag}', headers=headers)
manifest = manifest_req.json()
print(manifest)
registry_base_url = 'https://registry-1.docker.io'
if 'errors' in manifest:
print(f'Error downloading manifest: {manifest["errors"]}')
else:
# Create a folder for the image
os.makedirs(image, exist_ok=True)
# Download and store image configuration
config_req = requests.get(f'{registry_base_url}/v2/{image}/blobs/{manifest["config"]["digest"]}', headers=headers)
with open(f'{image}/config.json', 'wb') as config_file:
config_file.write(config_req.content)
# Download and store each layer
for i, layer in enumerate(manifest['layers']):
layer_req = requests.get(f'{registry_base_url}/v2/{image}/blobs/{layer["digest"]}', headers=headers)
with open(f'{image}/{i}.tar', 'wb') as layer_file:
layer_file.write(layer_req.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment