+91 9873530045
admin@learnwithfrahimcom
Mon - Sat : 09 AM - 09 PM

Lesson 6: Deploy to Cloud Run & Connect to Your Site

Vertex AI Chatbot Course – Lesson 6: Deploy to Cloud Run


Step A — Add a Dockerfile

At project root (same level as app/):

FROM python:3.11-slim

# Workdir
WORKDIR /app

# Copy files
COPY app/ /app/
COPY requirements.txt /app/requirements.txt

# Install deps
RUN pip install --no-cache-dir -r requirements.txt

# Expose port
ENV PORT=8080
EXPOSE 8080

# Flask needs host 0.0.0.0 in container
ENV GUNICORN_CMD_ARGS="--bind=0.0.0.0:8080 --workers=2 --threads=4"
CMD ["python", "main.py"]

Update the last line if you prefer Gunicorn:

CMD ["python","-m","gunicorn","main:app"]

Step B — Prepare for Cloud Run

  1. Make sure requirements.txt exists (from Lesson 3).
  2. Ensure your app binds to PORT=8080 in container:
    # in app/main.py end:
    if __name__ == "__main__":
        import os
        port = int(os.getenv("PORT", "8080"))
        app.run(host="0.0.0.0", port=port, debug=False)
  3. Never bake your service account JSON inside the image. Use Cloud Run’s IAM to attach a service account with Vertex permissions, or use Secret Manager.

Step C — Build & Deploy

Enable services (first time only):

gcloud services enable run.googleapis.com artifactregistry.googleapis.com

Create an Artifact Registry repo (once):

gcloud artifacts repositories create chat-repo --repository-format=docker --location=us --description="Chatbot images"

Build & push:

gcloud builds submit --tag us-docker.pkg.dev/PROJECT_ID/chat-repo/vertex-chat:latest

Deploy to Cloud Run (public URL):

gcloud run deploy vertex-chat \
  --image us-docker.pkg.dev/PROJECT_ID/chat-repo/vertex-chat:latest \
  --platform managed --region us-central1 \
  --allow-unauthenticated \
  --service-account vertex-chatbot-sa@PROJECT_ID.iam.gserviceaccount.com

Output includes a URL like: https://vertex-chat-xxxxx-uc.a.run.app.

Step D — Connect to Your Website

Option 1: Link/Embed — Add a menu link or embed via <iframe>:

<iframe src="https://vertex-chat-xxxxx-uc.a.run.app" width="100%" height="700" style="border:0"></iframe>

Option 2: Host UI on your domain, call Cloud Run API — Keep your static site on your hosting and POST to the Cloud Run service (enable CORS in Flask if needed).

# simple CORS in Flask
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)

Step E — Security Notes

  • Prefer attaching a service account to Cloud Run (no JSON key in container).
  • Use Secret Manager for API keys/secret config.
  • Set sensible quotas & timeouts in Cloud Run.