Thursday, April 17, 2025

 

Step by Step Guide: Deploying a FastAPI App to Google Cloud Run with a Custom Namecheap Domain


This walkthrough covers every step—from local setup to multi‑arch Docker builds on Apple Silicon, to pushing updates, to wiring up your Namecheap domain—so you can host your FastAPI app in Cloud Run under https://yourdomain.com.


Part 1: Initial Cloud Run Deployment


1. Install & Authenticate the Google Cloud SDK

  1. Download & install the Google Cloud SDK.

  2. Open a new terminal and run:

gcloud init

    • Sign in when prompted.

    • Select or create your GCP project.

  1. Enable required services:

gcloud services enable \
  run.googleapis.com \
  cloudbuild.googleapis.com \
  containerregistry.googleapis.com



2. Prepare Your FastAPI Code & Dockerfile


Your repo layout:

/concept_explorer
├─ backend/           ← FastAPI app + requirements.txt
│   └─ main.py
├─ frontend/          ← static assets (index.html, script.js, etc.)
└─ Dockerfile

Use this multi‑stage Dockerfile (listening on $PORT):

# syntax=docker/dockerfile:1

# 1) Build stage
FROM python:3.11-slim AS builder
WORKDIR /app
RUN apt-get update && apt-get install -y build-essential \
 && rm -rf /var/lib/apt/lists/*

COPY backend/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 2) Final image
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
COPY backend/ ./backend
COPY frontend/ ./frontend
EXPOSE 8080
CMD ["sh","-c","uvicorn backend.main:app --host 0.0.0.0 --port ${PORT:-8080}"]

3. Build & Push the Docker Image


a) On Intel / Linux hosts

export PROJECT_ID=your‑gcp‑project
export IMAGE_NAME=concept-explorer

docker build -t gcr.io/$PROJECT_ID/$IMAGE_NAME:latest .
docker push    gcr.io/$PROJECT_ID/$IMAGE_NAME:latest

b) On Apple Silicon (M1/M2) — Multi‑Arch Build

  1. Create and use a Buildx builder:

docker buildx create --name multiarch --use
docker buildx inspect --bootstrap


  1. Build & push for both amd64 and arm64:

docker buildx build \
  --platform linux/amd64,linux/arm64 \
  --tag gcr.io/$PROJECT_ID/$IMAGE_NAME:latest \
  --push \
  .



4. Deploy to Cloud Run

gcloud run deploy $IMAGE_NAME \
  --image gcr.io/$PROJECT_ID/$IMAGE_NAME:latest \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

You’ll see:

Service URL: https://concept-explorer-<hash>.run.app



Part 2: Redeploying After Code Changes


Whenever you update your FastAPI or frontend code:

  1. Rebuild & push (using the same multi‑arch steps if on Apple Silicon):

docker buildx build \
  --platform linux/amd64,linux/arm64 \
  --tag gcr.io/$PROJECT_ID/$IMAGE_NAME:latest \
  --push \
  .


  1. Redeploy:

gcloud run deploy $IMAGE_NAME \
  --image gcr.io/$PROJECT_ID/$IMAGE_NAME:latest \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated



Your Cloud Run service will automatically roll out the new revision.


Part 3: Mapping & Securing Your Namecheap Domain


1. Verify Domain Ownership in Google Search Console

  1. Go to Google Search Console.

  2. Add PropertyDomainyourdomain.com → follow DNS TXT verification instructions.

  3. Wait until Search Console shows Verified.


2. Create a Cloud Run Domain Mapping

gcloud beta run domain-mappings create \
  --service concept-explorer \
  --domain conceptexplorer.space \
  --platform managed \
  --region us-central1

Output will list the A and AAAA records you must add:

NAME                TYPE   CONTENTS
@                   A      206.289.32.91
@                   A      206.289.34.91
@                   A      206.289.36.91
@                   A      206.289.38.91
@                   AAAA   2001:4860:4812:52::15
@                   AAAA   2001:4860:4812:54::15
@                   AAAA   2001:4860:4812:56::15
@                   AAAA   2001:4860:4812:58::15

3. Configure Namecheap DNS


In your Namecheap domain’s Advanced DNS panel:

Type

Host

Value

TTL

A

@

206.289.32.91

Automatic

A

@

206.289.34.91

Automatic

A

@

206.289.36.91

Automatic

A

@

206.289.38.91

Automatic

AAAA

@

2001:4860:4812:52::15

Automatic

AAAA

@

2001:4860:4812:54::15

Automatic

AAAA

@

2001:4860:4812:56::15

Automatic

AAAA

@

2001:4860:4812:58::15

Automatic

Note: Host “@” means the root/apex domain.


4. Wait for Certificate Provisioning

  • Cloud Run provisions a Let’s Encrypt certificate automatically.

  • Propagation time: anywhere from 5 min up to 1 hr.

  • Check status in Cloud Console → Cloud Run → Domain Mappings; the STATUS column should turn READY.


5. Verify Everything

# Confirm DNS
dig +short A    conceptexplorer.space
dig +short AAAA conceptexplorer.space

# Confirm mapping status
gcloud beta run domain-mappings list \
  --platform managed \
  --region us-central1

When you see:

DOMAIN                 SERVICE           REGION      STATUS
conceptexplorer.space  concept-explorer  us-central1 READY

you can browse to https://conceptexplorer.space and your app will load securely.


Quick‑Reference Command Summary

# 1. Authenticate & select project
gcloud init
gcloud services enable run.googleapis.com cloudbuild.googleapis.com containerregistry.googleapis.com

# 2. Build & push image (Apple Silicon => multiarch)
docker buildx create --name multiarch --use
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  --tag gcr.io/$PROJECT_ID/concept-explorer:latest \
  --push \
  .

# 3. Deploy to Cloud Run
gcloud run deploy concept-explorer \
  --image gcr.io/$PROJECT_ID/concept-explorer:latest \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

# 4. Map custom domain (after Search Console verification)
gcloud beta run domain-mappings create \
  --service concept-explorer \
  --domain conceptexplorer.space \
  --platform managed \
  --region us-central1

# 5. Namecheap DNS: add the A & AAAA records provided by step 4.

# 6. Redeploy after code changes:
docker buildx build … --push …
gcloud run deploy …

Follow these steps in order and you’ll have your FastAPI app running on Cloud Run, reachable at https://yourdomain.com, with seamless updates and secure HTTPS.

No comments:

Post a Comment