How to Use Custom Domain with Cloud Run on GCP
To use a
custom domain with Cloud Run, first verify your domain ownership in Google Cloud Console, then map your domain to your Cloud Run service by creating a domain mapping. Finally, update your DNS records to point to the Cloud Run service using the provided CNAME or A records.Syntax
Using a custom domain with Cloud Run involves these main steps:
- Verify domain ownership: Confirm you own the domain in Google Cloud Console.
- Create domain mapping: Link your domain to your Cloud Run service.
- Update DNS records: Add DNS entries to your domain provider to route traffic to Cloud Run.
bash
gcloud domains verify --domain=example.com gcloud run domain-mappings create --service=my-service --domain=www.example.com
Example
This example shows how to map the custom domain www.example.com to a Cloud Run service named my-service in the us-central1 region.
bash
gcloud domains verify --domain=example.com
# After verification, create the domain mapping
gcloud run domain-mappings create --service=my-service --domain=www.example.com --region=us-central1
# The command outputs DNS records to add at your domain registrar, for example:
# CNAME record: ghs.googlehosted.com
# Update your DNS provider with the given records to complete setup.Output
Domain example.com verified successfully.
Created domain mapping for www.example.com to service my-service.
Common Pitfalls
- Not verifying domain ownership: You must verify your domain in Google Cloud Console before mapping.
- Incorrect DNS records: Adding wrong DNS entries will prevent your domain from routing to Cloud Run.
- Missing HTTPS setup: Cloud Run automatically provisions SSL certificates, but DNS must be correct first.
- Using unsupported domain formats: Only fully qualified domains (like www.example.com) are supported, not IP addresses.
bash
## Wrong way: skipping domain verification gcloud run domain-mappings create --service=my-service --domain=www.example.com # This will fail with an error about unverified domain. ## Right way: verify domain first gcloud domains verify --domain=example.com # Then create domain mapping successfully.
Quick Reference
| Step | Command / Action | Notes |
|---|---|---|
| 1 | Verify domain ownership | Use Google Cloud Console or `gcloud domains verify` |
| 2 | Create domain mapping | `gcloud run domain-mappings create --service=SERVICE --domain=DOMAIN` |
| 3 | Update DNS records | Add CNAME or A records as instructed by the command output |
| 4 | Wait for DNS propagation | May take minutes to hours |
| 5 | Access service via custom domain | HTTPS is auto-provisioned by Cloud Run |
Key Takeaways
Verify your domain ownership in Google Cloud before mapping it to Cloud Run.
Create a domain mapping linking your custom domain to your Cloud Run service.
Update your DNS provider with the exact records Cloud Run gives you.
Cloud Run automatically manages HTTPS certificates once DNS is correct.
Allow time for DNS changes to propagate before your domain works.