Manohar Kumar Singh
Manohar Kumar Singh

Reputation: 61

Error : Cloud Run error: Container failed to start. Stop Execution of Container/Cloud Run once execution of Python is done

My requirement is to stop the execution of Cloud run/container once the execution of python code is complete. The task is to fetch files from Cloud Storage, process them and then export them back to cloud storage. I am able to complete the task successfully. But the cloud build is ending with below error.

"deploy-to-cloud-run": ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more info.

CloudBuild.yml

- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/reponame:latest', '-t', 'gcr.io/$PROJECT_ID/reponame:$COMMIT_SHA', '-t', 'gcr.io/$PROJECT_ID/reponame:$BUILD_ID', '.']
  id: 'build-image-reponame'
  waitFor: ['-']  # The '-' indicates that this step begins immediately.
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/$PROJECT_ID/reponame:$COMMIT_SHA']
  id: 'push-image-to-container-registry'
  waitFor: ['build-image-reponame']
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args:
  - 'run'
  - 'deploy'
  - 'reponame'
  - '--image'
  - 'gcr.io/$PROJECT_ID/reponame:$COMMIT_SHA'
  - '--region'
  - 'us-east1'
  - '--platform'
  - 'managed'

  waitFor: ['push-image-to-container-registry']
  id: 'deploy-to-cloud-run'
images:
- 'gcr.io/$PROJECT_ID/reponame:latest'
- 'gcr.io/$PROJECT_ID/reponame:$COMMIT_SHA'
- 'gcr.io/$PROJECT_ID/reponame:$BUILD_ID'

Upvotes: 0

Views: 944

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 76000

You can simply rely on the getting started tutorial

For your code, do something like this (code get from the tutorial with some comments)

import os

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello_world():
    #name = os.environ.get("NAME", "World")
    #return "Hello {}!".format(name)
    # Put your logic here
    return "Done", 200 #return nicely the response to the request


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Then when you want to run your processing, call the / route and the process will be performed. That's all. If the processing take more than 3 minutes, you can increase the cloud run timeout up to 60 minutes.

Upvotes: 2

Related Questions