Deploy applications using Google Cloud Run

Google offers Serverless container deployment service called Google cloud run to deploy stateless application/ backend services. You can write your code using favorite languages like Go, python. Rust, .Net, Node.js or any other languages and deploy it to cloud run using Docker and Google SDK. This blog will explain about deployment of an example backend Node.js service. Since it is stateless / serverless we can't deploy persistence like databases, queues /streams engines, etc.

Google container registry is a place to store docker images ( like code repository ). First, we will push our container image to the registry and then will move/ deploy the image to the cloud run.

Install gcloud sdk using https://cloud.google.com/sdk/install and docker
login to gcloud sdk using

gcloud auth login

This will open up your browser window to authenticate google cloud account.
Let's create a Node.js app

mkdir gcloud-run
cd gcloud-run
npm init

add server.js file

touch server.js

Install express

npm install express --save

and add start script as node server.js inside package.json file. Now your package.json file will look like

{
  "name": "gcloud-run",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Cloud run default port is 8080

server.js file

const express = require('express')
const app = express()
const port = 8080

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Now let's start node app using npm start.

thatcoders-Macbook-pro ➜  gcloud-run npm start

> gcloud-run@1.0.0 start /Users/thatcoder/Desktop/gcloud-run
> node server.js

Example app listening on port 8080!

Ta da!

Now let's write Docker config file.
create Dockerfile in project root directory

FROM node:12-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV PORT 8080
# Run the web service on container startup.
CMD [ "npm", "start" ]

Now authenticate container registry

gcloud auth configure-docker

Now build your image using

docker build -f Dockerfile .

This will give you a tag like

thatcoders-Macbook-pro ➜ gcloud-run docker build -f Dockerfile .
Sending build context to Docker daemon  2.004MB
Step 1/7 : FROM node:12-alpine
 ---> 3fb8a14691d9
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 77c0ddabe2d0
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> efc2e1e7926f
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 5a6f86642d41
Step 5/7 : COPY . .
 ---> 6491e61438f4
Step 6/7 : ENV PORT 8080
 ---> Running in d79f8d9b9020
Removing intermediate container d79f8d9b9020
 ---> d55c77a948ce
Step 7/7 : CMD [ "npm", "start" ]
 ---> Running in 11afb55b7fd5
Removing intermediate container 11afb55b7fd5
 ---> e05d8df32052
Successfully built e05d8df32052

Copy the tag e05d8df32052 .
Now use command
docker tag e05d8df32052 us.gcr.io/project_name/testtest
where project_name is your google project name, testtest is the container name and us.gcr.io is the container registry region
Now push the container image to registry using
docker push us.gcr.io/project_name/testtest

Check container registry to see testtest  image.

Now open cloud run service inside console, and create new service

select container image as testtest

Allow authentication rule to public access and click on create button

Google will give you custom URL like https://testtest-ojbvbrqgeq-uc.a.run.app/

We can also setup custom domain using domain mapping inside cloud run


Happy Hacking ✌🏻

Youtube Video Tutorial:

Get the latest posts delivered right to your inbox.