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

Surender GuptaSurender Gupta
3 min read

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

  1. Build Docker image

  2. Push to Artifact Registry

  3. 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 πŸ™Œ

0
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.