I'm trying to write a python script to auto-delete old prerelease docker images that are taking up space in Google container registry. I'm stuck in how can I authenticate google cloud in python script and list docker images in GCR.
CodePudding user response:
Google Container Registry (GCR) implements (the de facto standard) Docker Registry API HTTP V2.
This means that you can use docker
, podman
and other tools that implement these APIs to interact with GCR and it means that you should use a 3rd-party client library (often provided by Docker) to implement interaction with a Docker Registry such as GCR.
Google (!) documents this on the page Docker Registry API
There is no Google API to interact with GCR.
You can demonstrate this to yourself by running e.g. gcloud container images list
with the --log-http
flag to show that the underlying REST calls are of the form http://gcr.io/v2/{repo}/tags/list
. This is documented on Docker's API page for GET tags
.
I've not used this but Docker provides a Docker SDK for Python and this should enable you to interact with any registry that implements the Docker Registry API HTTP V2 such as GCR.
You must authenticate to GCR using a suitable permitted Google identity. This is documented Authenticating using the Docker Registry API
CodePudding user response:
You can use the Google Cloud Python clients
in your Python script.
Then use a service account and a download a Json key from the GCP IAM page
.
You have to give the needed permissions to this service account in the IAM page.
Before to run the main of your Python
script, you can do an authentication on GCP with in your bash terminal :
export GOOGLE_APPLICATION_CREDENTIALS=your_path/your_service_account.json
To list your Docker
images, you can also use a shell
script instead of a Python
script. The following command list all the images of the current project from GCR
:
gcloud container images list
As explain before, in order to use gcloud
commands, you have to be authenticated with your service account or other identity.
https://cloud.google.com/sdk/gcloud/reference/container/images/list