Google Cloud Run — Deploying Containerized Applications to a Serverless Environment ⚡
Cloud Run is a managed compute platform that enables you to run stateless containers.
Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.
In this article, we would:
- Setup Cloud Shell
- Demo Project files and Dockerfile
- Build our Docker Image with Cloud Build and Push to Google Container Registry
- Deploy to Cloud Run from Google Container Registry using both Google Cloud Shell and Container Registry Interface
Setup Cloud Shell
I will be using Google Cloud Shell to manage resources on Google Cloud Platform with an assumption that you have it installed on your PC.
gcloud auth login gcloud config set project <PROJECT_ID> gcloud config set compute/zone us-central1-f gcloud services enable run.googleapis.com gcloud components install beta
where <PROJECT_ID> is your GCP project ID.
Demo Project files and Dockerfile
Our demo project files for this article would just be
package.json. We'll also have a
Dockerfile to build our image.
Demo Project Files on GitHub
Feel free to use your own project files.
We would not be building our Docker image on our PC, Google Cloud Build allows us to build a Docker image using a Dockerfile, which we already have and then pushes the image to Container Registry 😊
Build Docker Image with Cloud Build and Push to Google Container Registry
Let’s use Google Cloud Build to build our Docker image and push the image to Container Registry. Both can be done by simply running the following command:
gcloud builds submit --tag gcr.io/[PROJECT_ID]/quickstart-image .
That’s it! We have our Docker image built and now on Container Registry.
Docker Image on Container Registry
Note that if you’re building larger images, you can pass a timeout parameter such as:
Deploy to Cloud Run from Google Container Registry
We could either deploy from Cloud Shell or directly from Container Registry Interface.
Deploying from Cloud Shell
gcloud beta run deploy --image gcr.io/<PROJECT-ID>/quickstart-image
We’ll be asked to input Service name and some other options. On success, you’ll get the Service URL 😀
Deploying from Container Registry Interface
Click on the Image Name and Deploy the latest by selecting
Deploy to Cloud Run on the list of options.
Deploy to Cloud Run
We’ll also need to define the Service name and authentication option.
Cloud Run also allows us define additional options for our deployment such as Environment Variables , Memory Allocation.
Additional Options for Cloud Run
Create and take a sip of that juice. Voila!
Our Containerized Application Runs on Cloud Run - Serverlessly 😀
Cloud Run does not charge when the service is not in use. You can use a custom domain rather than the default address that Cloud Run provides for a deployed service.
Cloud Run also runs on Google Kubernetes Engine — this gives you more flexibility on managing your infrastructure. The tweet below gives more insights on this.
Thanks for reading through! Let me know if I missed any step, if something didn’t work out quite right for you or if this guide was helpful.