Python FastAPI Tutorial (Part 19): Deploy with Docker - Serverless Containers and Custom Domain

Corey Schafer
Corey SchaferApr 21, 2026

Why It Matters

Serverless container deployment cuts infrastructure overhead, improves security, and aligns costs with actual usage, accelerating FastAPI product releases.

Key Takeaways

  • Deploy FastAPI via Docker to Google Cloud Run for serverless scaling
  • Add security headers in FastAPI middleware to replace Nginx configuration
  • Use multi‑stage Dockerfile with pinned Python version for reproducible builds
  • Leverage UV package manager to pre‑compile dependencies and speed startup
  • Configure non‑root user in container to enhance runtime security

Summary

The video walks viewers through deploying a FastAPI application using Docker containers on Google Cloud Run, highlighting a shift from traditional VPS management to fully managed serverless infrastructure with automatic scaling, SSL, and pay‑as‑you‑go pricing. It also introduces Neon, a cloud‑hosted PostgreSQL service, eliminating the need for a self‑maintained database.

Key technical steps include adding a health‑check endpoint, implementing middleware to inject security headers (X‑Frame‑Options, X‑Content‑Type‑Options, Referrer‑Policy, HSTS) directly in the app, and installing Docker on various OSes. The tutorial then builds a multi‑stage Dockerfile: a builder stage that pins Python 3.14.4‑slim, pulls the UV binary, caches dependencies, and a production stage that copies only the necessary artifacts, creates a non‑root user, and runs the app.

The presenter demonstrates the middleware pattern with "app.middleware('http')" and shows Docker layer caching benefits by copying dependency files before source code. He also explains UV flags (‑‑locked, ‑‑no‑install‑project, ‑‑no‑dev) that ensure reproducible, lean builds, and verifies security headers via Chrome DevTools.

By moving to Cloud Run, developers gain zero‑maintenance scaling, reduced operational risk, and cost efficiency, while the containerized approach preserves portability across environments—from local laptops to Kubernetes clusters—making the workflow both secure and future‑proof.

Original Description

In this video, we'll be learning how to deploy our FastAPI application using Docker and Google Cloud Run. We'll containerize our app by writing a multi-stage Dockerfile, set up a serverless PostgreSQL database with Neon, deploy our container to Cloud Run, and configure a custom domain with HTTPS. We'll also add security headers to our application through middleware. This is a different approach from the VPS deployment in the previous video, where instead of managing our own server, Google handles the infrastructure for us and our app can scale to zero when nobody's using it. Let's get started...
The code from this video can be found here:
Full FastAPI Course:
Docker Desktop:
Create a New Google Cloud Project:
console.cloud.google.com/projectcreate
Google Cloud SDK:
NameCheap (with Referral):
Google Search Console:
search.google.com/search-console/
✅ Support My Channel Through Patreon:
✅ Become a Channel Member:
✅ One-Time Contribution Through PayPal:
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
✅ Equipment I Use and Books I Recommend:
▶️ You Can Find Me On:
My Website - http://coreyms.com/
#Python #FastAPI

Comments

Want to join the conversation?

Loading comments...