Overview and What You Will Learn
Artifact Registry is Google Cloud's managed storage for container images and language packages, and it replaces the older Container Registry. In this lab you will create a Docker repository, build and push images with Cloud Build, see how an overwritten tag leaves an untagged image behind, grant pull access at repository level, and configure cleanup policies safely with a dry run.
By the end you will be able to:
- Create a regional Docker repository and push images to it without a local Docker install.
- Explain how Artifact Registry differs from Container Registry.
- Write delete and keep cleanup policies, and test them before enforcing them.
- Grant a runtime identity read-only access to one repository instead of the whole project.
Why This Matters in Production
A registry that nobody cleans grows forever, and the bill rises quietly because nothing breaks. Picture a food-delivery platform where CI pushes an image on every merge: 200 builds a day is more than 70,000 image versions a year, almost all of which will never be pulled again. Storage cost, slow listings, and the risk that someone deletes an image a rollback still needs all come from the same missing habit: a cleanup policy with an explicit keep rule.
Core Principles
Artifact Registry stores artifacts in repositories, and each repository has one format, one location, and its own IAM policy. An image is addressed as LOCATION-docker.pkg.dev/PROJECT/REPOSITORY/IMAGE:TAG, and Cloud Run, GKE, and Compute Engine pull from that address.
| Feature | Container Registry | Artifact Registry |
|---|---|---|
| Formats | Docker images only | Docker, Helm, Maven, npm, Python, Go, apt, yum |
| Access control | Project and storage bucket level | Per-repository IAM |
| Cleanup | Manual scripts | Built-in cleanup policies |
| Status | Deprecated | Current service |
Dockerfile] -->|Submit build| CB[Cloud Build] CB -->|Push image| AR[(Artifact Registry
app-images)] AR -->|Pull image| CR[Cloud Run] AR -->|Pull image| GKE[GKE cluster] CP{Cleanup policy} -->|Delete stale| AR classDef src fill:#DBEAFE,color:#1E293B classDef gcp fill:#326CE5,color:#FFFFFF classDef pol fill:#F97316,color:#FFFFFF class S src class CB,AR,CR,GKE gcp class CP pol
A version is untagged when a newer push takes its tag. That happens constantly when pipelines push to a moving tag, and untagged versions are the safest target for automatic deletion.
Detailed Step-by-Step Practical Lab
You need the gcloud CLI, a billing account ID, and permission to create projects.
Step 1: Create a project and enable the APIs
export BILLING_ACCOUNT=0X0X0X-0X0X0X-0X0X0X gcloud projects create gcp-registry-lab-2026 --name="Artifact Registry Lab"gcloud billing projects link gcp-registry-lab-2026 --billing-account=$BILLING_ACCOUNTgcloud config set project gcp-registry-lab-2026gcloud services enable artifactregistry.googleapis.com cloudbuild.googleapis.comStep 2: Create a Docker repository in Mumbai
gcloud artifacts repositories create app-images \ --repository-format=docker \ --location=asia-south1 \ --description="Container images for application services"Step 3: Authenticate a local Docker client (optional) Cloud Build does not need this. It is only for pushing or pulling from your own machine.
gcloud auth configure-docker asia-south1-docker.pkg.devStep 4: Create a tiny sample application
mkdir checkout-service && cd checkout-service cat > Dockerfile << 'DOCKERFILE_EOF'FROM nginx:1.27-alpineCOPY index.html /usr/share/nginx/html/index.htmlDOCKERFILE_EOF echo "<h1>checkout-service build 1</h1>" > index.htmlStep 5: Build and push the first image with Cloud Build
gcloud builds submit \ --tag asia-south1-docker.pkg.dev/gcp-registry-lab-2026/app-images/checkout-service:v1 .TipIf the push fails with a permission error, grant
roles/artifactregistry.writerto the build service account named in the error message, then rerun the build.
Step 6: Overwrite the same tag to create an untagged version This simulates a pipeline that keeps pushing to one tag.
echo "<h1>checkout-service build 2</h1>" > index.html gcloud builds submit \ --tag asia-south1-docker.pkg.dev/gcp-registry-lab-2026/app-images/checkout-service:v1 . gcloud artifacts docker images list \ asia-south1-docker.pkg.dev/gcp-registry-lab-2026/app-images \ --include-tagsYou should see two digests for checkout-service, and only the newer one carries the v1 tag. The older digest is untagged, and it is exactly what a cleanup policy will target.
Step 7: Grant a runtime identity read-only access to this repository
gcloud iam service-accounts create checkout-runtime \ --display-name="Checkout runtime" gcloud artifacts repositories add-iam-policy-binding app-images \ --location=asia-south1 \ --member="serviceAccount:checkout-runtime@gcp-registry-lab-2026.iam.gserviceaccount.com" \ --role="roles/artifactregistry.reader"Step 8: Write cleanup policies with a delete rule and a keep rule
When both rules match a version, the keep rule wins. Durations are in seconds, so 30 days is 2592000s.
cat > cleanup-policy.json << 'CLEANUP_EOF'[ { "name": "delete-old-untagged", "action": {"type": "Delete"}, "condition": {"tagState": "untagged", "olderThan": "2592000s"} }, { "name": "keep-recent-versions", "action": {"type": "Keep"}, "mostRecentVersions": {"keepCount": 5} }]CLEANUP_EOFStep 9: Apply the policies in dry-run mode first Dry run records what would be deleted in Cloud Audit Logs without deleting anything.
gcloud artifacts repositories set-cleanup-policies app-images \ --location=asia-south1 \ --policy=cleanup-policy.json \ --dry-runWhen the audit logs show only versions you are happy to lose, enforce the policy:
gcloud artifacts repositories set-cleanup-policies app-images \ --location=asia-south1 \ --policy=cleanup-policy.json \ --no-dry-runRememberCleanup runs on a periodic schedule, not instantly, and your lab image is far younger than 30 days. You will not see it deleted during this lab, so the lab proves the configuration, not the deletion.
Step 10: Clean up the lab
cd .. && rm -rf checkout-service cleanup-policy.jsongcloud projects delete gcp-registry-lab-2026 --quietProduction Best Practices & Common Pitfalls
- Tag every build with an immutable identifier. Use the git commit SHA, and enable
--immutable-tagson release repositories, so you can always say exactly which code is running. - Pair every delete policy with a keep policy. A rule such as "keep the 5 most recent versions" or "keep anything tagged
v*" protects the images a rollback needs. - Place the repository near its consumers. A repository in the same region as your Cloud Run or GKE workloads pulls faster and avoids cross-region network charges.
- Scope IAM to the repository. Grant
roles/artifactregistry.readerto runtime identities androles/artifactregistry.writeronly to CI, never project-wide roles. - Scan images on push. Enable Artifact Analysis so known vulnerabilities in base images appear against each image.
- Cache upstream images with a remote repository. A remote repository for Docker Hub avoids rate limits and survives upstream outages.
Common MistakePushing an image on every commit with no cleanup policy, so thousands of unused versions pile up silently. A policy configured once keeps working, but only if it has a keep rule so a real release is never removed.
Common MistakeDeploying
latestto production. A moving tag makes rollbacks and audits guesswork, and it creates the untagged images that cleanup later removes.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud artifacts repositories create |
Create a repository |
gcloud builds submit --tag |
Build and push with Cloud Build |
gcloud artifacts docker images list --include-tags |
List images and their tags |
gcloud artifacts repositories set-cleanup-policies |
Apply cleanup policies, with --dry-run or --no-dry-run |
gcloud artifacts repositories add-iam-policy-binding |
Grant access on one repository |
| Symptom | Likely fix |
|---|---|
| Push fails with permission denied | Grant roles/artifactregistry.writer to the build or CI identity |
| Pull fails with 403 on Cloud Run or GKE | Grant roles/artifactregistry.reader to the runtime service account |
| Docker cannot find the repository host | Run gcloud auth configure-docker asia-south1-docker.pkg.dev |
| Cleanup deleted nothing | Policies run periodically, and versions must exceed olderThan |