How to Use Artifact Registry in GCP: Simple Guide
Use
Artifact Registry in GCP by creating a repository, then pushing and pulling container images or packages using gcloud commands or supported tools. It securely stores your artifacts and integrates with Google Cloud services for easy management.Syntax
The basic steps to use Artifact Registry in GCP include:
- Create a repository: Define where your artifacts will be stored.
- Configure authentication: Allow your local machine or CI/CD to access the registry.
- Push artifacts: Upload container images or packages to the repository.
- Pull artifacts: Download or use artifacts from the repository.
Commands use the gcloud artifacts group and Docker or language-specific tools.
bash
gcloud artifacts repositories create REPOSITORY_NAME --repository-format=docker --location=LOCATION --description="My Docker repo" gcloud auth configure-docker LOCATION-docker.pkg.dev docker tag IMAGE LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY_NAME/IMAGE_NAME:TAG docker push LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY_NAME/IMAGE_NAME:TAG
Example
This example shows how to create a Docker repository, authenticate, tag an image, and push it to Artifact Registry.
bash
PROJECT_ID=my-project LOCATION=us-central1 REPOSITORY_NAME=my-docker-repo IMAGE=nginx:latest # Create repository gcloud artifacts repositories create $REPOSITORY_NAME --repository-format=docker --location=$LOCATION --description="Docker repo" # Configure Docker authentication gcloud auth configure-docker $LOCATION-docker.pkg.dev # Tag the image docker tag $IMAGE $LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY_NAME/nginx:latest # Push the image docker push $LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY_NAME/nginx:latest
Output
Created repository [my-docker-repo] in location [us-central1].
Docker configuration updated for location us-central1-docker.pkg.dev.
The push refers to repository [us-central1-docker.pkg.dev/my-project/my-docker-repo]
...
latest: digest: sha256:... size: 1234
Common Pitfalls
- Not enabling the Artifact Registry API: You must enable it in your GCP project before use.
- Incorrect permissions: Ensure your user or service account has roles like
Artifact Registry ReaderorArtifact Registry Writer. - Wrong repository format: Choose the correct format (docker, maven, npm, etc.) when creating the repository.
- Authentication errors: Run
gcloud auth configure-dockerfor Docker or set up proper credentials for other package managers.
bash
## Wrong: Pushing without authentication # docker push us-central1-docker.pkg.dev/my-project/my-docker-repo/nginx:latest # Error: denied: Permission denied ## Right: Authenticate first # gcloud auth configure-docker us-central1-docker.pkg.dev # docker push us-central1-docker.pkg.dev/my-project/my-docker-repo/nginx:latest
Quick Reference
Remember these key commands and concepts when using Artifact Registry:
| Command | Purpose |
|---|---|
| gcloud artifacts repositories create | Create a new artifact repository |
| gcloud auth configure-docker | Set up Docker authentication for Artifact Registry |
| docker tag | Tag local Docker images for Artifact Registry |
| docker push | Push Docker images to Artifact Registry |
| docker pull | Pull Docker images from Artifact Registry |
Key Takeaways
Enable Artifact Registry API and create a repository before pushing artifacts.
Authenticate your environment using gcloud commands to avoid permission errors.
Use the correct repository format and location when creating repositories.
Tag your images or packages properly before pushing to Artifact Registry.
Artifact Registry integrates smoothly with Docker and other package managers.