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:

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:

Main Features:

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:

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:

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

  1. Use --no-allow-unauthenticated to restrict public access.

  2. Integrate with Identity-Aware Proxy (IAP) for secure internal endpoints.

Manage Environment Configuration

  1. Inject configuration using environment variables:

--set-env-vars ENV=production,DEBUG=False

  1. For sensitive data, consider using Google Secret Manager.

Improve Observability

  1. Cloud Run integrates with Cloud Logging and Cloud Monitoring.

  2. 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

  1. Use lightweight base images (e.g., python:3.10-slim, distroless)

  2. 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.