DEV Community

Cover image for Deploy NestJS on Google Cloud Run
Valentin Prugnaud
Valentin Prugnaud

Posted on • Originally published at on

Deploy NestJS on Google Cloud Run

Repository: WhatDaFox/nestjs-cloud-run-poc

Configure Google Cloud

To be able to build and deploy, you will need a Google Cloud project, with a billing account set up, as well as
the Google Cloud CLI installed.

Then you will need to create a configuration for your project:

$ gcloud config configurations create cloud-run
$ gcloud auth login # and follow the steps
$ gcloud config set project YOUR_PROJECT_ID
Enter fullscreen mode Exit fullscreen mode

Create the project

For this proof of concept, I will only use the default NestJS application, that contains a single endpoint / returning Hello world!:

$ npm i -g @nestjs/cli
$ nest new cloud-run
Enter fullscreen mode Exit fullscreen mode

Cloud Run will decide the port of our application, so we have to update the main.ts file to reference the PORT environment variable, like so:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT || 3000);
Enter fullscreen mode Exit fullscreen mode

Now we are ready to create the Dockerfile.

Create the Dockerfile

We need to containerize our application to be able to run on Cloud Run. Create a Dockerfile at the root of your project and copy/paste the following:

For better performance, I decided to build the app beforehand and run the start:prod command.

# Use the official lightweight Node.js 12 image.
FROM node:12-alpine

# Create and change to the app directory.
WORKDIR /usr/src/app

# Copy application dependency manifests in the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy local code to the container image.
COPY . ./

# Build the application
RUN npm run build

# Run the web service on container startup.
CMD [ "npm", "run", "start:prod" ]
Enter fullscreen mode Exit fullscreen mode

Build & Deploy

Now, we can use Cloud Build to build our docker image. Cloud Build will automatically detect our Dockerfile, build,
and push our image in Google Container Registry:

$ gcloud builds submit --tag
Enter fullscreen mode Exit fullscreen mode

Once that's done, we can run the following command to deploy our new revision to Cloud Run:

$ gcloud run deploy --image --platform managed
Enter fullscreen mode Exit fullscreen mode


When testing, I ran a small (to avoid crazy costs) benchmark with Apache Benchmark.

Here is the command I ran:

$ ab -n 1000 -c 80 https://cloud-run-url/
Enter fullscreen mode Exit fullscreen mode

Here are the results:

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd,
Licensed to The Apache Software Foundation,

Benchmarking cloud-run-url (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:        Google
Server Hostname:        cloud-run-url
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-CHACHA20-POLY1305,2048,256
Server Temp Key:        ECDH X25519 253 bits
TLS Server Name:        cloud-run-url

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      80
Time taken for tests:   8.624 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      486004 bytes
HTML transferred:       12000 bytes
Requests per second:    115.95 [#/sec] (mean)
Time per request:       689.939 [ms] (mean)
Time per request:       8.624 [ms] (mean, across all concurrent requests)
Transfer rate:          55.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       61  402 219.1    375    2652
Processing:    29  207 117.5    192    1328
Waiting:       24  168 114.6    146    1279
Total:        163  609 236.4    567    2819

Percentage of the requests served within a certain time (ms)
  50%    567
  66%    622
  75%    681
  80%    714
  90%    804
  95%    920
  98%   1221
  99%   1754
 100%   2819 (longest request)
Enter fullscreen mode Exit fullscreen mode


It is pretty straightforward to build and deploy a container to Cloud Run. The response time can sometimes be pretty slow,
but overall, if the container is small and quick to start, it should run pretty smoothly.

Top comments (0)