Developing your service

This page describes a few things you need to know to get started in developing a service for Cloud Run.

Code requirements

You must meet the following requirements when you develop a service:

  • The service must listen for requests. You can configure the port on which requests are sent. Inside Cloud Run instances, the value of the PORT environment variable always reflects the port to which requests are sent. Your code should check for the existence of this PORT environment variable and if it is present, should listen on it to maximize portability.
  • The service must be stateless. It cannot rely on a persistent local state.
  • If the service performs background activities outside the scope of request handling it must use the instance-based billing setting in order to have CPU allocated outside of the standard request cycle.
  • If your service uses a network file system, it must use the second generation execution environment.

You can find more details about these constraints in the Container runtime contract.

Programming language support

Cloud Run source deployment conveniently supports multiple language runtimes, each of which is available through container base images provided by Google Cloud's buildpacks. You can also use other programming languages or frameworks, as long as the source code is in an OCI-compliant image.

The build and deploy quickstarts provides samples in many popular languages.

Using a web server

You can use a web server to listen on the required port, and to process and route incoming requests. For example, Node.js developers can use Express.js, Python developers can use Flask, Ruby developers can use Sinatra, and so forth.

Containerizing your code

See Containerize your code for details.

Design Cloud Run apps with Gemini assistance

You can get AI-powered help from Gemini Cloud Assist chat to design applications optimally. With Gemini Cloud Assist, you can discover the most suitable configurations and strategies for deploying your applications on Cloud Run, ensuring efficient resource utilization and seamless operation.

To use Gemini Cloud Assist from the Google Cloud console, do the following:

  1. Ensure that Gemini Cloud Assist is set up for your Google Cloud user account and project.
  2. Set up your Cloud Run development environment in your Google Cloud project and ensure you have the appropriate deployment permissions.

  3. Go to the Cloud Run page in the Google Cloud console.

    Go to Cloud Run

  4. In the console toolbar, select a Google Cloud project. Use a project associated with a project ID you submitted after you were granted access to Gemini Cloud Assist.

  5. Click spark Open or close Gemini AI chat.

    The Gemini panel opens.

  6. If necessary, click Accept if you agree to the terms.

  7. If you have a question about a specific application, provide context by going to the page that shows your resource before asking your question. When generating a response, Gemini includes information about the current console page and project.

  8. Enter a prompt in the Gemini panel.

    The following table provide some example prompts for using Gemini Cloud Assist with Cloud Run.

    Prompt Type of response
    "I want to design a Node.js app on Cloud Run. What's the recommended approach and what do I need to know?" Summary of containerizing your app, structuring your node.js app, and the deployment process.
    "What permission do I need to deploy a Cloud Run app?" List of required permissions and roles.
    "I want to deploy a three-tier web application using Cloud Run" General outline of the steps to design a three-tier web app.
    "How do I migrate my App Engine app to Cloud Run" High-level overview of key steps for preparing your Cloud Run development environment, and some key differences between both products. Links to an example tutorial that can be used to customize the solution to meet your specific needs.
    "What is the gcloud CLI command for updating the min instance setting to 2 for my Cloud Run service 'example-service'" Suggested gcloud CLI command for updating the min instances setting.
    "How do I update my service account for my Cloud Run service hello-world" Suggested gcloud CLI command for updating the service account.
    "Show me an example of building a generative AI app on Cloud Run with LangChain" Steps for creating an example app using Cloud Run with LangChain.
    "Generate a Terraform config using the google_cloud_run_v2_service resource for a Cloud Run service that sets the billing setting to instance-based billing" Suggested Terraform configuration for updating the billing setting.

For more details, see the following resources:

What's next