About Me
Projects
Notes
Online Content
Resume
© 2026 /Hadi Rouhani
BlogMarch 1, 2026

Mapping a Custom Domain to Google Cloud Run with Porkbun

Avatar
Hadi Rouhani
Mapping a Custom Domain to Google Cloud Run with Porkbun

Overview

This guide shows you how to map both yourdomain.com and www.yourdomain.com to a Cloud Run service using Porkbun DNS, with Google-managed HTTPS certificates. Prerequisites:
  • Cloud Run service deployed and accessible at *.run.app
  • Domain registered with Porkbun
  • GCP project with billing enabled

Verify Domain Ownership

  1. Go to Cloud Run → Domain mappings → Add mapping
  2. Select your service and enter your domain (e.g., hadirouhani.com)
  3. Google provides a TXT verification record
  4. In Porkbun DNS, add:
    • Type: TXT
    • Host: @
    • Value: google-site-verification=<your-token>
    • TTL: 600
  5. Return to Cloud Run and complete verification

Create Domain Mappings

Create two separate mappings in Cloud Run:
  1. yourdomain.com (apex)
  2. www.yourdomain.com
Cloud Run will show the exact DNS records needed for each.

Configure DNS in Porkbun

For www subdomain:

  • Type: CNAME
  • Host: www
  • Answer: ghs.googlehosted.com
  • TTL: 600

For apex domain:

Add four A records:
  • Type: A, Host: @, Answer: 216.239.32.21
  • Type: A, Host: @, Answer: 216.239.34.21
  • Type: A, Host: @, Answer: 216.239.36.21
  • Type: A, Host: @, Answer: 216.239.38.21
Add four AAAA records:
  • Type: AAAA, Host: @, Answer: 2001:4860:4802:32::15
  • Type: AAAA, Host: @, Answer: 2001:4860:4802:34::15
  • Type: AAAA, Host: @, Answer: 2001:4860:4802:36::15
  • Type: AAAA, Host: @, Answer: 2001:4860:4802:38::15
Important: Use the exact IPs shown in your Cloud Run domain mapping, not these examples.

Verify DNS Propagation

Bash
# Check apex A records
dig +short yourdomain.com A

# Check www CNAME
dig +short www.yourdomain.com CNAME

# Test HTTPS
curl -Iv https://www.yourdomain.com
curl -Iv https://yourdomain.com

HTTP to HTTPS Redirect

Cloud Run doesn't automatically redirect HTTP → HTTPS. Add this to your app: Node.js/Express:
Javascript
app.use((req, res, next) => {
  if (req.headers['x-forwarded-proto'] !== 'https') {
    return res.redirect(301, `https://${req.headers.host}${req.url}`);
  }
  next();
});
Python/FastAPI:
Python
@app.middleware("http")
async def https_redirect(request: Request, call_next):
    if request.headers.get("x-forwarded-proto") != "https":
        url = request.url.replace(scheme="https")
        return RedirectResponse(str(url), status_code=301)
    return await call_next(request)

Common Issues

www works, apex doesn't:
  • Verify apex A/AAAA records match Cloud Run exactly
  • Remove any conflicting ALIAS records
  • Try IPv4 only: curl -4Iv https://yourdomain.com
Connection closed / 502 errors:
  • DNS records don't match Cloud Run requirements
  • Mapping status not "Ready" yet (check Cloud Run console)
  • Remove wildcard CNAME records (* → ghs.googlehosted.com)
Email stopped working:
  • Restore MX records for your email provider (Porkbun forwarding uses fwd1.porkbun.com and fwd2.porkbun.com)

Final Checklist

Before going live, verify:
  • Cloud Run service responds at *.run.app URL
  • Domain verified with TXT record
  • Both apex and www mappings created in Cloud Run
  • Porkbun DNS has all required A/AAAA and CNAME records
  • No wildcard or conflicting records
  • dig confirms correct DNS resolution
  • Domain mapping status shows "Ready" in Cloud Run
  • HTTPS works for both apex and www
  • MX records preserved if using email
Share this post:

Recent posts

Thumbnail of End-to-End Workflow: From Development to Production on Google Cloud
Avatar
Hadi Rouhani
March 1, 2026
End-to-End Workflow: From Development to Production on Google CloudDevOps