Does it make sense to run a non web application on cloud run?

Viewed 1898

I see that all of the examples per the documentation use some form of a simple web application (For example, Flask in Python). Is it possible to use cloud run as a non web application? For example, deploy cloud run to use a python script and then use GCP Scheduler to invoke cloud run every hour to run that script? Basically my thinking for this is to avoid having to deploy and pay for Compute Engine, and only pay for when the cloud run container is invoked via the scheduler.

3 Answers

It's mandatory to answer to HTTP request. It's the contract of Cloud Run

  • Stateless (no volume attached to the container)
  • Answer to HTTP request

However, if you already have a python script, it's easy to wrap it in a flask webserver. Let's say, you have something like this (I assume that the file name is main.py -> important for the Dockerfile at the end)

import ....

var = todo(...)
connect = connect(...)
connect(var)

  1. Firstly, wrap it in a function like this
import ....

def my_function(request):
  var = todo(...)
  connect = connect(...)
  connect(var)
  return 'ok',200

  1. Secondly, add a flask server
from flask import Flask, request
import os
import ....

app = Flask(__name__)

@app.route('/')
def my_function(request):
  var = todo(...)
  connect = connect(...)
  connect(var)
  return 'ok',200

if __name__ == "__main__":
  app.run(host='0.0.0.0',port=int(os.environ.get('PORT',8080)))
  1. Add flask in your requirements.txt
  2. Build a standard container, here an example of Dockerfile
FROM python:3-alpine

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .
ENV PORT 8080

CMD [ "python", "main.py" ]
  1. Build (with Cloud Build for example) and deploy the service on Cloud Run

Now you have an URL, that you can call with Cloud Scheduler.

Be careful, the max request duration is, for now, limited to 15 minutes (soon 4x more) and limited to 2vCPU and 2Gb of memory (again, soon more).

It depends what is being installed in the container image, as there is no requirement that one would have to install a web-server. For example, with such an image I can build Android applications, triggered whenever a repository changes (file excludes recommend) ...and likely could even run a head-less Android emulator for Gradle test tasks and publish test results to Pub/Sub (at least while the test-suite wouldn't run for too long). I mean, one has to understand the possibilities of Cloud Build to understand what Cloud Run can do.

I've struggled with deployment my function which no need to handle any request in Cloud Run by putting functions in Flask app and found out that Cloud Run provides us 2 kinds of jobs, services and jobs.

Illustration from codelabs

From cloud run jobs documentation,

This page describes how to create and update Cloud Run jobs from an existing container image. Unlike services, which listen for requests, a job does not serve requests but only runs its tasks and exits when finished. After you create or update a job, you can execute the job as a one-off, on a schedule or as part of a workflow. You can manage individual job executions and view the execution logs.

You may see that there are two tabs in Cloud Run console. I am not sure when Cloud Run jobs started. See cloud run console

Related