cat > skopeo_pull.py << \EOF
import subprocess
import argparse
def split_docker_image(image: str, prefix=""):
image_parts = image.split("/")
name_tag = image_parts[-1]
if len(image_parts) >= 3:
# docker.io/library/mysql:5.7.44
project = image_parts[-2]
hub = "/".join(image_parts[:-2])
elif len(image_parts) == 2:
# win7/mysql:5.7.44
project = image_parts[0]
hub = "docker.io"
else:
# mysql:5.7.44
project = "library"
hub = "docker.io"
if len(name_tag.split(":")) == 1:
name = name_tag
tag = "latest"
else:
name, tag = name_tag.split(":")
if prefix != "":
name = name[len(prefix):]
return hub, project, name, tag
def main():
parser = argparse.ArgumentParser(description="Process docker image.")
parser.add_argument('-i','--image', type=str, help='Docker image to be processed')
parser.add_argument('-p','--prefix', type=str, default="", help='Prefix to remove from the image name')
parser.add_argument('-d','--download-path', type=str, default="/pool2/Downloads/", help='Path where the docker image will be downloaded')
parser.add_argument('-r','--dry-run', action='store_true', help='If set, the commands will not be executed')
args = parser.parse_args()
hub, project, name, tag = split_docker_image(args.image, args.prefix)
file_path = f"{args.download_path}{name}_{tag}.tar.gz"
# Ensure the download path ends with a slash
if not args.download_path.endswith('/'):
args.download_path += '/'
image = f"{hub}/{project}/{name}:{tag}"
download_cmd = f"skopeo copy docker://{image} docker-archive:/{file_path}:{image}"
load_cmd = f"docker load -i {file_path}"
delete_cmd = f"rm -rf {file_path}"
print(f"{download_cmd}")
print(f"{load_cmd}")
print(f"{delete_cmd}")
if not args.dry_run:
subprocess.run(download_cmd, shell=True)
subprocess.run(load_cmd, shell=True)
if __name__ == "__main__":
main()
EOF
pull docker image with skopeo
2024-04-20