Getting Started with Google Cloud Run: Deploying Python Scripts

dharmendra mishra
3 min readOct 16, 2023

--

Introduction

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.

Prerequisites

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 app.py:

from flask import Flask

app = Flask(__name)

@app.route(‘/’)

def hello_world():

return ‘Hello, Cloud Run!’

if __name__ == ‘__main__’:

app.run(host=’0.0.0.0', 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

WORKDIR /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 pypi.python.org -r requirements.txt

# Make port 8080 available to the world outside this container

EXPOSE 8080

# Define environment variable

ENV NAME World

# Run app.py when the container launches

CMD [“python”, “app.py”]

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

PROJECT_ID=your-project-id

REGION=us-central1

IMAGE_NAME=my-python-app

TAG=v1

# Build the container image

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

# Push the container image to Google Container Registry

docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:$TAG

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 \

— image gcr.io/$PROJECT_ID/$IMAGE_NAME:$TAG \

— platform managed \

— region $REGION

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

Conclusion

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
dharmendra mishra

Written by 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.

No responses yet