π From Zero to Hero: Deploying a FastAPI Backend on Google Cloud Run with CI/CD, Docker & MongoDB


Hey Devs! π
This week, I took on a fun but powerful challenge β containerizing a FastAPI backend, wiring up MongoDB, pushing to Google Cloud Run, and fully automating it with GitHub Actions. And let me tell you β it feels GOOD to ship like a pro πͺ
Hereβs how I did it (and how you can too).
π§ TL;DR
π FastAPI backend for a logging/incident tracking service
π Dockerized and deployed to Google Cloud Run
π MongoDB Atlas for persistence
π GitHub Actions for zero-touch deployments
π Full CI/CD pipeline using Artifact Registry + Cloud Run
π Production-ready, scalable, and auto-deployed on every push!
π§ Tech Stack
FastAPI β blazing fast Python framework
Docker β containerizing like a boss
MongoDB Atlas β cloud-hosted database
Google Cloud Run β serverless container deployment
GitHub Actions β CI/CD pipelines for automation
Google Artifact Registry β to store and manage container images
π» What I Built
A REST API to log incidents/alerts that:
Accepts structured JSON data via POST
Stores it in a MongoDB database
Returns clean, structured responses
Has built-in Swagger UI (/docs)
π Live Demo: π https://fastapi-agent-154172965587.us-central1.run.app (Play around with /docs β it's interactive!)
π³ Dockerization in a Nutshell
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
Simple. Fast. Efficient.
βοΈ Deployment to Google Cloud Run
Build Docker image
Push to Artifact Registry
Deploy to Cloud Run via GitHub Actions
I configured service accounts, IAM roles, and tokens securely to allow the GitHub pipeline to deploy like magic. β¨
βοΈ CI/CD with GitHub Actions
name: Deploy to Cloud Run
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true
- name: Docker build & push
run: |
docker build -t REGION-docker.pkg.dev/PROJECT_ID/REPO/fastapi-backend .
docker push REGION-docker.pkg.dev/PROJECT_ID/REPO/fastapi-backend
- name: Deploy to Cloud Run
run: |
gcloud run deploy fastapi-backend \
--image REGION-docker.pkg.dev/PROJECT_ID/REPO/fastapi-backend \
--platform managed \
--region REGION \
--allow-unauthenticated
π Secrets are managed via GitHubβs encrypted secret manager.
π Full Deep Dive (Step-by-Step)
π I broke down every single detail in this Medium article:
π How I Containerized and Deployed a FastAPI Backend on Google Cloud Run (Like a Pro)
π» Source Code on GitHub
Browse, fork, and β the repo here:
π github.com/surendergupta/task_backend_devops
π Final Thoughts
β
Cloud Run made scaling effortless
β
GitHub Actions made CI/CD painless
β
FastAPI made building APIs fun again
β
Docker made everything consistent
If you're a Python/DevOps/cloud enthusiast, I highly recommend trying this stack combo out. Youβll learn a ton!
π Letβs Connect
If you enjoyed this, leave a β€οΈ or drop a comment below! Got questions? Iβm happy to help or collaborate.
Letβs build cool things together π
Subscribe to my newsletter
Read articles from Surender Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Surender Gupta
Surender Gupta
PHP Developer with 7+ years of experience, now diving deep into DevOps for 1+ year. Skilled in Laravel, MERN, Docker, Kubernetes, and cloud platforms (AWS/GCP). I love building robust apps and automating deployments to make development smoother and faster. Sharing my journey, tips, and tutorials along the way.