Search code examples
dockergoogle-cloud-platformgcloudgoogle-container-registry

How Would I List All Google Container Registry Hostnames with the Cloud SDK?


How could I list all the different Google Container Registry (GCR) hostnames found within a given Google Cloud Platform (GCP) project using the Cloud SDK (gcloud)?

I.e., how would I list all the different GCR hostnames (e.g., gcr.io, us.gcr.io, etc..,.) for a GCP project $GCP_PROJECT_ID?


Solution

  • You are unable to use the list command underneath the images subgroup [and possibly apply filters or transforms] because it only lists repositories underneath the gcr.io hostname:

    gcloud container images list
    
    #=>
    
    gcr.io/$GCP_PROJECT_ID/. . .
    . . .
    Only listing images in gcr.io/$GCP_PROJECT_ID. Use --repository to list images in other repositories.
    

    Attempting to apply a wildcard (*) yields the following unfavorable results:

    • gcloud container images list --repository="*.gcr.io"
      
      #=>
      
      ERROR: (gcloud.container.images.list) A Docker registry domain must be specified.
      
    • gcloud container images list --repository="*.gcr.io/*"
      
      #=>
      
      ERROR: (gcloud.container.images.list) Invalid repository: *, must be at least 2 characters
      
    • gcloud container images list --repository="*.gcr.io/**"
      
      #=>
      
      ERROR: (gcloud.container.images.list) Invalid repository: **, acceptable characters include: abcdefghijklmnopqrstuvwxyz0123456789_-./
      
    • gcloud container images list --repository="*.gcr.io/$GCP_PROJECT_ID"
      
      #=>
      
      ERROR: (gcloud.container.images.list) *.gcr.io/$GCP_PROJECT_ID is not in a supported registry.  Supported registries are ['gcr.io', 'us.gcr.io', . . ., 'mirror.gcr.io', 'k8s.gcr.io']
      

    Trying to use the list command for the services group:

    gcloud services list \
    --filter="name=projects/$GCP_PROJECT_NUMBER/services/containerregistry.googleapis.com" \
    --format=yaml \
    --project=$GCP_PROJECT_ID
    

    or the search-all-resources command for the asset group:

    gcloud asset search-all-resources \
    --format=yaml \
    --filter="name=//serviceusage.googleapis.com/projects/$GCP_PROJECT_NUMBER/services/containerregistry.googleapis.com" \
    --scope=projects/$GCP_PROJECT_ID
    

    doesn't reveal any information about active hostnames either.


    The only solution seems to be piping the output of the list command against the asset group into uniq:

    gcloud asset list \
    --filter="assetType=containerregistry.googleapis.com/Image" \
    --format="value(name.split('/').slice(3, 4))" \
    --project=$GCP_PROJECT_ID | uniq
    
    #=>
    
    gcr.io
    us.gcr.io
    . . .
    mirror.gcr.io
    k8s.gcr.io