Build and deploy serverless dockerized API with Cloud Run

Shivanshu Patel
May 9 · 3 min read

At Next’19 event google has launched it’s new service/solution Cloud Run for running serverless container(Docker) based application. Cloud Run is a managed platform that allows to run stateless containers that are invocable via HTTP requests. Compare to serverless function platform(Lambda, cloud/azure function etc) here we can write code in any language and libraries, don’t need to depend on platform for providing support for choice of language. Cloud run is also autoscaling and works with cloud build so no need to worry about scaling and provisioning.

Lets start by building simple http nodeJs API

$ mkdir node-api
$ cd node-api
$ npm init -y
$ touch index.js

now create a http server with an endpoint which return some data.

Now let’s Dockerize this API

$ touch Dockerfile

First build and test it locally before go live :)

$ docker build -t shiv/node-api .
$ docker run -p 49160:8082 -d shiv/node-api
$ curl -i localhost:49160
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 09 May 2019 13:24:35 GMT
Connection: keep-alive
Content-Length: 16
{"text":"Hello"}%

Lets Deploy it to Cloud Run

# Get  GCP project ID where we wants to publish this api
$ gcloud config get-value project
# Build container image using Cloud Build, will take few seconds
$ gcloud builds submit --tag gcr.io/[PROJECT-ID]/node-api

Upon success, you will see a SUCCESS message containing the image name (gcr.io/[PROJECT-ID]/node-api). The image is stored in Container Registry.

Lets deploy it using below command

# Will ask few question, allow unaunthenticated user so we can see it in browser$ gcloud beta run deploy --image  gcr.io/[PROJECT-ID]/node-apiDo you want to continue (Y/n)?  yService name (node-api): Allow unauthenticated invocations to new service [node-api]?(y/N): y

Success : Service [node-api] revision [node-api-00001] has been deployed and is serving traffic at https://some-auto-generated-url.

All Done! Visit your deployed container by opening the service URL in a web browser.

You can also see container up and running through GCP console. Login and visit Cloud run. Node-api is up and running and it’s serverless, auto scaling.

Don’t forget to delete service once you are done with testing

Few things to consider

This service is still in beta mode.

It’s based on knative so other cloud provider soon provide similar service.

Cloud run is available on GKE too it has different pricing model though

There is some requirements for the container’s contract, Have a look here

this is my first post on medium so please help me with my mistakes :)

And bit Marketing stuff

I am full-stack developer based in London, For more information please visit : http://shivanshupatel.com

Shivanshu Patel

Written by

Fullstack developer based in London, Enjoying Working with Java, Javascript(NodeJs, React), PHP. Learning Cloud technologies (Serverless, Kubnernet).

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade