Cloud Run: deploy a simple express.js app

Cloud Run: deploy a simple express.js app What is Cloud Run? In Google Cloud Platform we have several PaaS services that are key for DevOps deployments. Cloud Run is a fully managed container deployment platform, it let us run containerized

Source comparison and review notes
  • Automated pair-review warning (AI bilingual content review): The Container Registry commands and gcr.io workflow are legacy relative to Artifact Registry; preserve the historical source but treat this deployment guidance as time-sensitive.

What is Cloud Run?

In Google Cloud Platform we have several PaaS services that are key for DevOps deployments. Cloud Run is a fully managed container deployment platform, it let us run containerized applications without having to take care of servers configuration. We only need to have our container ready to be deployed.

Cloud Run its a serverless offering that enables us to run statless containers that can be invoked. As the Docs explain it lets you “run your frontend and backend services, batch jobs, and queue processing workloads. Optimized for developer productivity and fast container startup”

Cloud Run advantajes

  • Automatic Scaling: It scales the number of containers up or down based on traffic, ensuring that you are only using (and paying for) what you need.
  • Containerized: it can support any programming language and tool that you can containerize, offering greater flexibility.
  • CI/CD Integration: It integrates well with continuous integration and deployment pipelines, making it easier to manage deployments and track them through to production.
  • Cost-Effective: you pay only for the resources you useand it includes a free quota.

Let’s start with the deployment of our sample js app:

Enviroment Setup

In this case, we’ll make use of Cloud Shell in a GCP sample project. First of all we’ll need to set up the project that we’ll be working with:

Bash
gcloud config set project <your-project-id>

Enable de Cloud Run API:

Bash
gcloud services enable run.googleapis.com

Setup compute region:

Bash
gcloud config set compute/region us-central1

Then, we’ll make us of variables in the shell and save the location

Bash
LOCATION="us-central1"
echo $LOCATION

Now we are going to clone the sample js app in this Repo

Bash
git clone https://github.com/mdiloreto/cloud-run-js-simple-app
cd cloud-run-js-simple-app
ls

Build Container Image

Now we are going to build the image to Container Registry with Cloud Build using the following gcloud command:

Bash
gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld

$GOOGLE_CLOUD_PROJECT: is an environment variable that holds your project id. It's used here to specify the project within the Google Container Registry where the image will be stored.

After running the last command we will be able to see that the image was created using the following cmd:

Bash
gcloud container images list

Deploy the image to Cloud Run

Now we are going to deploy the container image to Cloud Run using the following cmd:

Bash
gcloud run deploy --image gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld --allow-unauthenticated --region=$LOCATION

The flag --allow-unauthenticated: sets the deployed service to be accessible without authentication. This means that anyone on the internet can access the service.

The green arrowin the image indicates the url where the Cloud Run app its accesible:

¿Continous Deployment?

Cloud Run itself does not automatically implement continuous deployment; it is designed to run containerized applications and does not by default monitor your container registry for updates. However, you can integrate Cloud Run with Google Cloud Build or other CI/CD tools to achieve continuous deployment.

For example we could use:

Thanks for reading. The learning continues in the next article.