Introduction
Deploying modern Python web APIs has become easier than ever thanks to frameworks like FastAPI and serverless container platforms like Google Cloud Run. In this post we walk through how to build a FastAPI application, wrap it in a Docker container, push it to Google’s container registry, and deploy to Cloud Run. We’ll use a reference article as a base structure.
By the end of this tutorial you’ll have a running FastAPI service accessible on the web, and you’ll understand how the pieces fit together.
Why use FastAPI and Cloud Run?
FastAPI – modern, fast, Python-based
-
FastAPI allows you to build APIs with very good performance, automatic interactive docs (OpenAPI/Swagger) and type hints.
-
It’s increasingly popular for production-quality APIs in Python.
-
For example, you might build an endpoint that receives JSON, processes it, and returns a result — something that integrates well with containerized deployment.
Cloud Run – serverless containers on Google Cloud
-
Google Cloud Run lets you deploy containers that scale automatically, you pay only for what you use, and you don’t need to manage servers.
-
It supports arbitrary containers, so your FastAPI + Docker image easily fits in.
-
The reference tutorial shows the full flow: write the FastAPI code, build the Docker image, push to Google Container Registry (GCR), then deploy to Cloud Run.
Combining FastAPI + Cloud Run gives you: fast development cycles, scalable deployment, minimal ops, and the flexibility of containers.
Prerequisites
Before diving in, make sure you have the following:
-
A Google Cloud account and a project set up.
-
gcloudcommand line tool installed and configured (Google Cloud SDK). Read Medium articles -
Docker installed locally (so you can build images).
-
Basic familiarity with Python, FastAPI, and Docker.
-
Billing enabled for your GCP project (Cloud Run may incur charges).
-
Optionally: enable relevant Google Cloud APIs (Container Registry / Artifact Registry, Cloud Run, etc).
Step-by-Step Deployment Guide
Step 1: Write your FastAPI application
Create a file main.py (or whatever you choose) with something like this:
Also create requirements.txt, e.g.:
Step 2: Create a Dockerfile
In the same directory, create a Dockerfile, for example:
Note that Cloud Run uses port 8080 by default (you can override, but this is convenient).
Step 3: Enable Container Registry / Artifact Registry
gcloud CLI if you haven’t yet. Step 4: Build & push the Docker image
Build locally:
Tag the image for your project’s registry:
Authenticate Docker to GCP (if needed):
Push the image:
(Replace your-project-id with your real project ID.)
Step 5: Deploy to Cloud Run
Use the gcloud command to deploy:
Step 6 (optional): Setup Pub/Sub trigger
If you want to trigger your service via a message queue (e.g., Google Cloud Pub/Sub), you can create a topic and push subscription pointing to your Cloud Run URL:
Then publish a message:
And check the logs of your Cloud Run service to see if the message was received.
Step 7: Test & Monitor
-
Visit the service URL to send HTTP requests and verify responses.
-
Use
gcloud logging reador the Google Cloud Console Logging viewer to view logs. Example given in reference: -
Check concurrency, latency, error rates; adjust memory, CPU, concurrency settings in Cloud Run if needed.
Best practices & Tips
-
Port & environment variables: FastAPI listens on port 8080 (standard for Cloud Run) unless changed.
-
Concurrency: Cloud Run allows multiple requests per container instance by default (depending on settings). Optimize
uvicorn(workers, threads) accordingly. -
Scaling & cost control: Cloud Run scales to zero when idle — good for cost. Set maximum instances, revise memory/CPU to control pricing.
-
Security: Don’t always use
--allow-unauthenticatedunless public access is required; consider authentication via IAM or IAP for secure APIs. -
Logging & monitoring: Use structured logs, error reporting, and alerting. Cloud Logging integrates with Cloud Run.
-
Container size & layers: Keep your image as small as possible (python-slim helps). Use multi-stage builds if your build process is heavy.
-
CI/CD: Consider automating build & deploy via Cloud Build or GitHub Actions, triggered on commits.
-
Environment variables & secrets: Use Secret Manager or environment variables for credentials, avoid hard-coding.
-
Health check endpoints: Enable a
/healthroute in your FastAPI app for readiness/liveness probes if needed (especially for migrations or long startup). -
Resource limits: Adjust memory/CPU in Cloud Run depending on your app’s CPU & memory usage, especially if handling large payloads or concurrency.
Common Pitfalls & How to Avoid Them
| Pitfall | How to avoid / fix |
|---|---|
| Container fails to start or times out | Ensure CMD is correct, app listens on 0.0.0.0:8080, dependencies installed. |
| Image too large / slow cold start | Use slim base image, remove build dependencies, combine RUN commands. |
Authentication required but --allow-unauthenticated used | Use IAM roles, Service Account, or IAP rather than making public if you need secure endpoints. |
| Logs not appearing | Ensure the container writes logs to stdout/stderr; use proper log levels; check correct resource filter in Logging. |
| Receiving error from Pub/Sub push | Ensure the push endpoint URL is correct (including trailing slash if needed), that your service is set to allow unauthenticated (or set correct identity), and ack-deadline is appropriate. |
| Unexpected cost | Monitor usage, set max instances, minimize idle instances, review scaling settings. |
Conclusion
Deploying a FastAPI application on Google Cloud Run is a powerful way to deliver performant, scalable APIs without heavy infrastructure management. By combining:
-
FastAPI for rapid, efficient Python APIs
-
Docker containers for portability
-
Cloud Run for serverless containers
fastapi deployment, cloud run fastapi, docker fastapi, google cloud run tutorial, python fastapi cloud, serverless fastapi, containerize fastapi, fastapi api deployment guide, gcp fastapi deployment

Comments
Post a Comment