Package: moby-distribution
moby-distribution - Yet another moby(docker) distribution implement by python.moby-distribution is a library for operating Docker Image Manifest and Blobs (Layers, Config, etc.).Works for Python 3.6+.UsageInstallYou can install from PyPi.❯pipinstallmoby-distributionOr install from GitHub for latest version.❯pipinstallhttps://github.com/shabbywu/distribution/archive/main.zipIntroductionThe API provides several classes:ManifestRef,Blob,Tags,DockerRegistryV2Client,APIEndpoint,ImageRefManifestRefhas the following methods:get(media_type)retrieve image manifest as the provided media_typeget_metadata(media_type)retrieve the manifest descriptor if the manifest exists.delete(raise_not_found)Removes the manifest specified by the provided reference.put(manifest)creates or updates the given manifest.Blobhas the following methods:download(digest)download the blob from registry tolocal_pathorfileobjupload()upload the blob fromlocal_pathorfileobjto the registry by streamingupload_at_one_time()upload the monolithic blob fromlocal_pathorfileobjto the registry at one time.mount_from(from_repo)mount the blob from the given repo, if the client has read access to.delete(digest)delete the blob at the registry.Tagshas the following methods:list()return the list of tags in the repoget(tag)retrieve the manifest descriptor identified by the tag.untag(tag)work likeManifestRef.delete()ImageRefhas the following methods:from_image(from_repo, from_reference, to_repo, to_reference)init aImageReffrom{from_repo}:{from_reference}but will name{to_repo, to_reference}.save(dest)save the image to dest, as Docker Image Specification v1.2 Format.push(media_type="application/vnd.docker.distribution.manifest.v2+json")push the image to the registry.push_v2()push the image to the registry, with Manifest Schema2.add_layer(layer_ref)add a layer to this image, this is a way to build a new Image.DockerRegistryV2Clienthas the following methods:from_api_endpoint(api_endpoint, username, password)initial a client to theapi_endpointwithusernameandpasswordAPIEndpointis a dataclass, you can define APIEndpoint in the following ways:frommoby_distributionimportAPIEndpoint# 1. Provide schemeAPIEndpoint(url="https://registry.hub.docker.com")# 2. No scheme providedAPIEndpoint(url="registry.hub.docker.com")if the scheme is missing, we will detect whether the server provides ssl and verify the certificate.If no ssl: use http(80).
If have ssl, but certificate is invalid:try to ping the registry with https(443), if success, use itotherwise, downgrade to http(80)
If have ssl and valid certificate: use https(443)We provide an anonymous client connected to Docker Official Registry as default, you can find it atmoby_distribution.default_client,
and you can override the default client byset_default_client(client).Example1. List Tags for the Docker Official Imagelibrary/pythonfrommoby_distributionimportTagsTags(repo="library/python").list()# ['2', '2-alpine', '2-alpine3.10', '2-alpine3.11', '2-alpine3.4', '2-alpine3.6', ...]2. Get Manifest for the Docker Official Imagelibrary/python:latestfrommoby_distributionimportManifestRef,ManifestSchema1,ManifestSchema2# Get Docker Image Manifest Version 2, Schema 1# see alse: https://github.com/distribution/distribution/blob/main/docs/spec/manifest-v2-1.mdManifestRef(repo="library/python",reference="latest").get(ManifestSchema1.content_type())# Get Docker Image Manifest Version 2, Schema 2# see alse: https://github.com/distribution/distribution/blob/main/docs/spec/manifest-v2-2.mdManifestRef(repo="library/python",reference="latest").get(ManifestSchema2.content_type())3. Get the Config(aka Image JSON) for the Docker Official Imagelibrary/python:latestfromioimportBytesIOfrommoby_distributionimportManifestRef,Blob# Get Docker Image Manifest Version 2, Schema 2manifest=ManifestRef(repo="library/python",reference="latest").get()fh=BytesIO()Blob(fileobj=fh,repo="library/python",digest=manifest.config.digest).download()fh.seek(0)config=fh.read()Using thelocal_pathparameter, you can download to the file system instead of memory.4. Push Blobs (Layers, Config, etc.) to Docker RegistryfromioimportBytesIOfrommoby_distributionimportBlob,DockerRegistryV2Client,OFFICIAL_ENDPOINT# To upload files to Docker Registry, you must login to your accountclient=DockerRegistryV2Client.from_api_endpoint(OFFICIAL_ENDPOINT,username="your-username",password="your-password")fh=BytesIO("just a demo")assertBlob(repo="your-username/demo",fileobj=fh).upload()Using thelocal_pathparameter, you can upload blobs from the file system instead of memory.5. Push Image Manifest to Docker Registryfrommoby_distributionimportManifestSchema2,DockerRegistryV2Client,OFFICIAL_ENDPOINT,ManifestRef# To upload files to Docker Registry, you must login to your accountclient=DockerRegistryV2Client.from_api_endpoint(OFFICIAL_ENDPOINT,username="your-username",password="your-password")# Build the ManifestSchema2 you need to uploadmanifest=ManifestSchema2(schemaVersion=2,mediaType="application/vnd.docker.distribution.manifest.v2+json",config={...},layers=[...])ManifestRef(repo="your-username/demo",reference="latest").put(manifest)6. Upload the complete image to Docker Registry, Do it Yourself!Read the process description ofthe official documentPushing the Layers as Example 4 doPushing the Config as Example 4 doPushing the Image Manifest as Example 5 doDone, Congratulations!Here is another way, use the newly implemented ImageRef!frommoby_distributionimportImageRef,DockerRegistryV2Client,OFFICIAL_ENDPOINT# To upload files to Docker Registry, you must login to your accountclient=DockerRegistryV2Client.from_api_endpoint(OFFICIAL_ENDPOINT,username="your-username",password="your-password")image_ref=ImageRef.from_image(from_repo="your-repo",from_reference="your-reference",to_reference="the-new-reference")image_ref.push()The above statement achieves the equivalent function ofdocker tag {your-repo}:{your-reference} {your-repo}:{the-new-reference} && docker push {your-repo}:{the-new-reference}RoadMapimplement the Distribution Client API for moby(docker)implement the Docker Image Operator(Operator that implement Example 6)Command line tool for operating Imageimplement the Distribution Client API for OCI