Pair Cloud Run with Firebase Hosting to generate and serve your dynamic content or build REST APIs as microservices.
Using Cloud Run , you can deploy an application packaged in a container image. Then, using Firebase Hosting , you can direct HTTPS requests to trigger your containerized app.
- Cloud Run supports several languages (including Go, Node.js, Python, and Java), giving you the flexibility to use the programming language and framework of your choice.
- Cloud Run automatically and horizontally scales your container image to handle the received requests, then scales down when demand decreases.
- You only pay for the CPU, memory, and networking consumed during request handling.
For example use cases and samples for Cloud Run integrated with Firebase Hosting , visit our serverless overview .
This guide shows you how to:
- Write a simple Hello World application
- Containerize an app and upload it to Artifact Registry
- Deploy the container image to Cloud Run
- Direct Hosting requests to your containerized app
Note that to improve the performance of serving dynamic content, you can optionally tune your cache settings .
Before you begin
Before using Cloud Run
, you need to complete some initial tasks,
including setting up a Cloud Billing
account, enabling the Cloud Run
API, and installing the gcloud
command line tool.
Set up billing for your project
Cloud Run offers free usage quota , but you still must have a Cloud Billing account associated with your Firebase project to use or try out Cloud Run .
Enable the API and install the SDK
-
Enable the Cloud Run API in the Google APIs console:
-
Open the Cloud Run API page in the Google APIs console.
-
When prompted, select your Firebase project.
-
Click Enableon the Cloud Run API page.
-
-
Install and initialize the Cloud SDK.
-
Check that the
gcloud
tool is configured for the correct project:gcloud config list
Step 1: Write the sample application
Note that Cloud Run supports many other languages in addition to the languages shown in the following sample.
Go
-
Create a new directory named
helloworld-go
, then change directory into it:mkdir helloworld-go
cd helloworld-go
-
Create a new file named
helloworld.go
, then add the following code:This code creates a basic web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be containerized and uploaded to Artifact Registry .
Node.js
-
Create a new directory named
helloworld-nodejs
, then change directory into it:mkdir helloworld-nodejs
cd helloworld-nodejs
-
Create a
package.json
file with the following contents: -
Create a new file named
index.js
, then add the following code:This code creates a basic web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be containerized and uploaded to Artifact Registry .
Python
-
Create a new directory named
helloworld-python
, then change directory into it:mkdir helloworld-python
cd helloworld-python
-
Create a new file named
app.py
, then add the following code:This code creates a basic web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be containerized and uploaded to Artifact Registry .
Java
-
Install Java SE 8 or later JDK and CURL .
Note that we only need to do this to create the new web project in the next step. The Dockerfile, which is described later, will load all dependencies into the container.
-
From the console, create a new empty web project using cURL then unzip commands:
curl https://start.spring.io/starter.zip \ -d dependencies=web \ -d name=helloworld \ -d artifactId=helloworld \ -o helloworld.zip
unzip helloworld.zip
This creates a SpringBoot project.
-
Update the
SpringBootApplication
class insrc/main/java/com/example/helloworld/HelloworldApplication.java
by adding a@RestController
to handle the/
mapping and also add a@Value
field to provide theTARGET
environment variable:This code creates a basic web server that listens on the port defined by the
PORT
environment variable.
Your app is finished and ready to be containerized and uploaded to Artifact Registry .
Step 2: Containerize an app and upload it to Artifact Registry
-
Containerize the sample app by creating a new file named
Dockerfile
in the same directory as the source files. Copy the following content into your file.Go
Node.js
Python
Java
-
Build your container image using Cloud Build by running the following command from the directory containing your Dockerfile:
gcloud builds submit --tag gcr.io/ PROJECT_ID /helloworld
Upon success, you will see a SUCCESS message containing the image name
(gcr.io/ PROJECT_ID /helloworld
).
The container image is now stored in Artifact Registry and can be re-used if desired.
Note that, instead of Cloud Build , you can use a locally installed version of Docker to build your container locally .
Step 3: Deploy the container image to Cloud Run
Allowed Cloud Run regions
For the best performance, colocate your Cloud Run service with Hosting using the following regions:
-
us-west1
-
us-central1
-
us-east1
-
europe-west1
-
asia-east1
Rewrites to Cloud Run from Hosting are supported in the following regions:
-
asia-east1
-
asia-east2
-
asia-northeast1
-
asia-northeast2
-
asia-northeast3
-
asia-south1
-
asia-south2
-
asia-southeast1
-
asia-southeast2
-
australia-southeast1
-
australia-southeast2
-
europe-central2
-
europe-north1
-
europe-southwest1
-
europe-west1
-
europe-west12
-
europe-west2
-
europe-west3
-
europe-west4
-
europe-west6
-
europe-west8
-
europe-west9
-
me-central1
-
me-west1
-
northamerica-northeast1
-
northamerica-northeast2
-
southamerica-east1
-
southamerica-west1
-
us-central1
-
us-east1
-
us-east4
-
us-east5
-
us-south1
-
us-west1
-
us-west2
-
us-west3
-
us-west4
-
us-west1
-
us-central1
-
us-east1
-
europe-west1
-
asia-east1