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 🙌