简体   繁体   中英

Tag image in Google Container Registry with Docker API for Python

I have a project in Google Cloud and I want to tag image in Container Registry with new_tag. I wrote code:

import docker

cli = docker.from_env()
img = cli.images.get('my_image:latest')
tagged = img.tag('my_image:new_tag')
print('image tagged: ' + str(tagged))

How could I set arguments for docker.from_env such as host (gcr.io), project (my_project) and authentication with google cloud service account?

Write a python program that aks user to input 10 integrrs . It then performs the following : -prints maximum , minimum number in the list -prints largest odd number in the list .if no odd number in the input ,print "No odd Number" For example : Input result 10 120 20 5 30 101 40 5 6 7 90 101 120

2 100 4 2 6 no odd number 8 10 20 30 40 50 80 100

As Google Container Registry is a remote registry, it is not possible to directly add a tag there from the local system other than using gcloud as mentioned here .

There is a way to add a new tag by running a python script. For that you have to -

  1. Pull the image from Google Container Registry
  2. Add a new tag to it locally
  3. Push the newly tagged image to Google Container Registry

The following python script works well for this task :

import docker
client = docker.from_env()
base_url = "gcr.io/project-id/demo-image"
#existing tag of the image in gcr
from_url=":tag1"
#desired tag to be added
to_tag = ":tag2"
#pull the image from container registry to local system
client.images.pull(base_url+from_url)
#get the image from local system and add a new tag to it
client.images.get(base_url+from_url).tag(base_url+to_tag)
#push the newly tagged image to container registry
client.images.push(base_url+to_tag)
#remove the pulled image from local system
client.images.remove(base_url+to_tag)
#remove the newly tagged image from local system
client.images.remove(base_url+from_url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM