on
From Code to Cloud with Python on Cloud Run
Recently, I had the opportunity to work with Google Cloud Run on a project, and it significantly transformed my approach to application deployment and scalability. Whether you’re exploring Google Cloud for the first time or looking to optimize your deployment strategy, Cloud Run offers a robust, serverless platform tailored for modern developers.
In this blog, I’ll cover:
- An overview of Google Cloud Run and its benefits
- Step-by-step instructions for deploying a Flask-based Python application
- Advanced deployment tips and best practices for production readiness
What is Google Cloud Run?
Google Cloud Run is a service that runs containerized applications without needing you to manage servers. You package your application into a Docker container, and Cloud Run takes care of the rest, such as:
- Scaling the application up and down based on traffic
- Handling incoming requests and balancing the load
- Managing security, monitoring, and splitting traffic between different versions
Main Features:
- Scales Automatically: Your application handles more or fewer requests as needed, including scaling down to zero when there is no traffic.
- Works with Any Language: You can deploy any application that responds to HTTP requests, no matter what language or framework you use.
- Pay for Use: You only pay when your application is running and handling requests.
- No Server Management: You do not need to set up or maintain servers or Kubernetes clusters.
Cloud Run lets you use containers while removing the need to manage the infrastructure. It is useful for testing ideas or running full applications.
Prerequisites
Before we begin, ensure the following tools and services are set up:
- A Google Cloud Platform (GCP) account (free tier is sufficient for this tutorial)
- The Google Cloud CLI (gcloud) installed and authenticated
- Docker installed on your local machine
Step-by-Step Guide: Deploying a Python Flask Application
We’ll create a simple web application using Flask and deploy it to Google Cloud Run.
Step 1: Create a Simple Flask Application
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Cloud Run!"
Create a requirements.txt file to list the dependencies:
flask==2.2.5
gunicorn==21.2.0
Step 2: Add a Dockerfile
Next, let’s create a Dockerfile to define the environment and how the app should run.
# Use an official Python runtime as a base image
FROM python:3.10-slim
# Set the working directory
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Set the command to run the app
CMD ["gunicorn", "-b", ":8080", "app:app"]
Important: Cloud Run expects the app to listen on port 8080.
Step 3: Build and Push Your Container
Now, use Google Cloud Build to build the Docker image and push it to Google Container Registry (or Artifact Registry, depending on your setup).
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/cloud-run-demo
Replace YOUR_PROJECT_ID with your actual GCP project ID.
This step might take a minute — it’s building the image, uploading it, and storing it securely.
Step 4: Deploy to Cloud Run
With the container image ready, the next step is to deploy the application to Cloud Run.
gcloud run deploy cloud-run-demo \
--image gcr.io/YOUR_PROJECT_ID/cloud-run-demo \
--platform managed \
--region us-central1 \
--allow-unauthenticated
This command performs the following actions:
-
Deploys the service named cloud-run-demo.
-
Specifies the container image stored in Google Container Registry.
-
Targets the fully managed Cloud Run platform.
-
Sets the deployment region to us-central1.
-
Allows unauthenticated access to the service.
After deployment, Cloud Run will provide a public URL where the application can be accessed.
Visit the URL — you should see “Hello from Cloud Run!”
Advanced Cloud Run Configuration and Best Practices
To build production-grade applications on Cloud Run, it is important to follow best practices for security, configuration management, observability, deployment, and performance optimization.
Secure Your Application
-
Use
--no-allow-unauthenticated
to restrict public access. -
Integrate with Identity-Aware Proxy (IAP) for secure internal endpoints.
Manage Environment Configuration
- Inject configuration using environment variables:
--set-env-vars ENV=production,DEBUG=False
- For sensitive data, consider using Google Secret Manager.
Improve Observability
-
Cloud Run integrates with Cloud Logging and Cloud Monitoring.
-
Set up alerts based on error rates, latency, or traffic spikes.
Implement Continuous Deployment
Automate deployment workflows with Cloud Build Triggers or GitHub Actions:
- name: Deploy to Cloud Run
run: gcloud run deploy ...
This ensures that changes are automatically deployed after successful builds.
Optimize Your Containers
-
Use lightweight base images (e.g., python:3.10-slim, distroless)
-
Reduce container startup time for faster cold starts
Conclusion
We started by building a Python application, creating a container image, and uploading it to Google Container Registry. Then, we deployed the container to Cloud Run using the gcloud command-line tool. We also covered best practices such as securing your service, managing environment variables, setting up monitoring, and optimizing container performance. Understanding these steps gives you a strong foundation for using Cloud Run in real projects. Once you are comfortable with basic deployments, you can explore advanced features like setting up custom domains, connecting to private networks (VPC connectors), and enabling service-to-service communication. Cloud Run can support many types of applications. It manages the infrastructure so you can focus on writing and improving your code.
If you have any questions or would like to share your experience, feel free to reach out.