Google Cloud Run Jobs for background tasks

Shubhangi Thakur
3 min readJul 5, 2023

--

In this blog, I will guide you on how to create service and jobs in Cloud Run on the Google Cloud platefrom. I will provide a detailed, step-by-step demonstration of how to service and jobs by deploying a sample application to the Google Cloud Run.

What is Cloud Run ?

Cloud Run is a serverless compute platform provided by Google Cloud Platform (GCP). It allows developers to deploy and run applications in containers without the need to manage underlying infrastructure. Cloud Run abstracts away the server and operating system management, enabling developers to focus on building and deploying their applications.

Cloud Run supports applications packaged in Docker containers, making it compatible with a wide range of programming languages and frameworks. You can build your application using any language or framework of your choice, containerize it using Docker, and then deploy it to Cloud Run.

What is Jobs in cloud run ?

A job refers to a type of workload that you can run on the serverless platform. Cloud Run allows you to deploy and run stateless containers that automatically scale based on incoming requests. While the primary use case for Cloud Run is to handle HTTP requests for web applications and APIs, you can also use it to run background tasks or batch jobs.

Cloud Run provides a flexible and scalable platform for running jobs in a serverless manner. It offers the benefits of automatic scaling, pay-per-use pricing, and simplified infrastructure management.

Let’s see ..

To create jobs for an application on Cloud Run, you need to create a Docker image of your application and then deploy that image to Cloud Run.

Setp-1. I will create sample docker image using Dockerfile.

This is sample application:

import os

def main():
task_index = os.getenv('CLOUD_RUN_TASK_INDEX')
task_count = os.getenv('CLOUD_RUN_TASK_COUNT')

print(f'CLOUD_RUN_TASK_INDEX: {task_index}')
print(f'CLOUD_RUN_TASK_COUNT: {task_count}')


if __name__ == '__main__':
main()

Dockerfile:

FROM python:3.8-slim-buster
WORKDIR /app
ADD . /app
CMD ["python", "app.py"]

Now build this image using this command:

docker build -t <give image name> .

Step-2. Upload created image to Artifact registry:

gcloud auth login
docker tag <give image name> us-central1-docker.pkg.dev/<project-id>/<repo-nmae>/<image-name>:latest
docker push us-central1-docker.pkg.dev/<project-id>/<repo-nmae>/<image-name>:latest

Step-3. Go to Cloud Run

You can consider the below configuration or create your own according to your use case and then click on done.

  • Click to create job
  • Now click on select then left dialog box will open and select your image in drop-down list and click on select
  • Give Number of tasks according to your use case
  • SelectExecute job immediately
  • Click on Create

Step-4. Now job successfully created on cloud run

  • Click on execute then job will be executed.

Thanks for reading this, hope it helped you in some way. Happy Learning on Google Cloud.

Read: Deploy services on Google Cloud Run

--

--

Shubhangi Thakur

Hi, I am Student | Cloud Engineer | GCP+AWS Cloud | DevOps | 2XGCP Certified