Getting Started with Google Cloud Run: Deploying Python Scripts

dharmendra mishra
3 min readOct 16, 2023


Google Cloud Run is a fully managed compute platform that automatically scales your Python scripts in containers. It allows developers to run their code in containers, deploy it in a serverless environment, and easily scale applications as needed. In this article, we’ll explore how to use Google Cloud Run to deploy and run Python scripts, making it an ideal platform for building and deploying web services, APIs, and more.


Before you begin, make sure you have the following:

  1. A Google Cloud Platform (GCP) account.
  2. Google Cloud SDK installed on your local machine.
  3. A basic understanding of Python and containerization.

Getting Started:

Let’s dive into deploying a Python script on Google Cloud Run. We’ll go through the following steps:

  1. Writing a Python Script
  2. Creating a Container Image
  3. Deploying the Image on Google Cloud Run

Step 1: Writing a Python Script

For this example, we’ll create a simple Python script that serves an HTTP response using Flask. First, create a Python file named

from flask import Flask

app = Flask(__name)


def hello_world():

return ‘Hello, Cloud Run!’

if __name__ == ‘__main__’:’', port=8080)

This script uses the Flask web framework to create a basic web server that responds with “Hello, Cloud Run!” when accessed.

Step 2: Creating a Container Image

To deploy your Python script on Google Cloud Run, you need to containerize it. Create a Dockerfile in the same directory as your Python script:

# Use the official Python runtime as a parent image

FROM python:3.9-slim

# Set the working directory to /app


# Copy the current directory contents into the container at /app

COPY . /app

# Install any needed packages specified in requirements.txt

RUN pip install — trusted-host -r requirements.txt

# Make port 8080 available to the world outside this container


# Define environment variable


# Run when the container launches

CMD [“python”, “”]

This Dockerfile uses an official Python 3.9 image, sets up the working directory, copies the Python script and requirements file, installs dependencies, exposes port 8080, and runs the Python script.

Step 3: Deploying the Image on Google Cloud Run

Before deploying the container image, you need to build and push it to a container registry. Google Cloud provides a Container Registry service, but you can also use other container registries like Docker Hub.

To build the image and push it to Google Container Registry, use the following commands:

# Set environment variables





# Build the container image

docker build -t$PROJECT_ID/$IMAGE_NAME:$TAG .

# Push the container image to Google Container Registry


Make sure to replace your-project-id with your GCP project ID and set a suitable image name and tag.

With the container image pushed to the registry, you can now deploy it on Google Cloud Run:

# Deploy the container image to Cloud Run

gcloud run deploy $IMAGE_NAME \


— platform managed \

— region $REGION

During deployment, you will be prompted to allow unauthenticated access. Choose the “Allow all” option.


You have successfully deployed a Python script as a containerized application on Google Cloud Run. This serverless platform allows you to run your Python scripts with ease, automatically handling scaling and load balancing. It is powered by Knative.



dharmendra mishra

Data-driven Analytics/Engineering leader with 12+ years of experience in digital advertising company. Skills SQL, Python, Excel and GCP Google Cloud Platform.