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 Container 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 Enable on 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 Container 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 Container 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 Container 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 Container Registry.
Step 2: Containerize an app and upload it to Container 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 Container 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
Deploy using the following command:
gcloud run deploy --image gcr.io/PROJECT_ID/helloworld
When prompted:
- Select a region (for example
us-central1
) - Confirm the service name (for example,
helloworld
) - Respond
Y
to allow unauthenticated invocations
- Select a region (for example
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
Wait a few moments for the deploy to complete. On success, the command line displays the service URL. For example:
https://helloworld-RANDOM_HASH-us-central1.a.run.app Visit your deployed container by opening the service URL in a web browser.
The next step walks you through how to access this containerized app from a Firebase Hosting URL so that it can generate dynamic content for your Firebase-hosted site.
Step 4: Direct hosting requests to your containerized app
With rewrite rules, you can direct requests that match specific patterns to a single destination.
The following example shows how to direct all requests from the page
/helloworld
on your Hosting site to trigger the startup and running of
your helloworld
container instance.
Make sure that:
You have the latest version of the Firebase CLI.
You have initialized Firebase Hosting.
For detailed instructions about installing the CLI and initializing Hosting, see the Get Started guide for Hosting.
Open your
firebase.json
file.Add the following
rewrite
configuration under thehosting
section:"hosting": { // ... // Add the "rewrites" attribute within "hosting" "rewrites": [ { "source": "/helloworld", "run": { "serviceId": "helloworld", // "service name" (from when you deployed the container image) "region": "us-central1", // optional (if omitted, default is us-central1) "pinTag": true // optional (see note below) } } ] }
Deploy your hosting configuration to your site by running the following command from the root of your project directory:
firebase deploy --only hosting
With this feature, you can ensure that the revision of your Cloud Run service for generating your site's dynamic content is kept in sync with your static Hosting resources and Hosting config. Also, this feature allows you to preview your rewrites to Cloud Run on Hosting preview channels.
If you add
"pinTag": true
to arun
block of thehosting.rewrites
config, your static Hosting resources and configuration will be pinned to the most recent revision of the Cloud Run service, at the time of deploy. If you roll back a version of your site, the revision of the "pinned" Cloud Run service is also rolled back.This feature relies on Cloud Run tags, which have a limit of 1000 tags per service and 2000 tags per region. This means that after hundreds of deploys, the oldest versions of a site may stop working.
Your container is now reachable via the following URLs:
Your Firebase subdomains:
PROJECT_ID.web.app/
andPROJECT_ID.firebaseapp.com/
Any connected custom domains:
CUSTOM_DOMAIN/
Visit the Hosting configuration page for more details about rewrite rules. You can also learn about the priority order of responses for various Hosting configurations.
Test locally
During development, you can run and test your container image locally. For detailed instructions, visit the Cloud Run documentation.
Next steps
Set up caching of your dynamic content on a global CDN.
Interact with other Firebase services using the Firebase Admin SDK.
Learn more about Cloud Run, including detailed how-to guides for setting up, managing, and configuring containers.
Review the pricing and the quotas and limits for Cloud Run.