Skip to main content

Deploy a Python app

This guide covers deploying a Python backend (FastAPI, Flask, or Django) to Cloud Run with advncd.


Prerequisites

  • advncd installed and authenticated (advncd login)
  • A GCP project configured (advncd init)
  • Required APIs enabled (advncd apis enable)

1. Check detection

advncd detect --path .

Expected output for a FastAPI app:

runtime:              python
build strategy: buildpacks
port: 8080
confidence: high
warnings: none
service name proposal: my-api

2. Make sure your app binds to 0.0.0.0 and reads PORT

Cloud Run injects the PORT environment variable.

FastAPI (with uvicorn):

import os
import uvicorn
from fastapi import FastAPI

app = FastAPI()

if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
uvicorn.run(app, host="0.0.0.0", port=port)

Or in your Procfile:

web: uvicorn main:app --host 0.0.0.0 --port $PORT

Flask:

import os
from flask import Flask

app = Flask(__name__)

if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port)

3. requirements.txt

Make sure requirements.txt exists at the project root — Cloud Buildpacks uses it to install dependencies:

fastapi
uvicorn[standard]

4. Deploy

advncd deploy --path . --name my-python-api

5. Verify

advncd services describe my-python-api
advncd services open my-python-api

advncd.yaml

version: 1

service:
name: my-python-api
port: 8080

deploy:
project: my-gcp-project
region: europe-west1
allow_service_rename: false

build:
strategy: buildpacks

runtime:
family: python
framework: fastapi

With this in the project root:

advncd deploy

Django notes

For Django, ensure your ALLOWED_HOSTS includes the Cloud Run domain:

ALLOWED_HOSTS = [
os.environ.get("CLOUDRUN_SERVICE_URL", "localhost"),
".run.app",
]

And use a Procfile for the entry point:

web: gunicorn myproject.wsgi --bind 0.0.0.0:$PORT