Google Cloud Platform(GCP) — Dockerize a Node.js Web App and Deploy to Compute Engine Instance

Kadir Emre Ozcan
6 min readApr 7, 2020

Plan of the post:

1. Create Node.js Web App

2. Create a Dockerfile

3. Install Google Cloud SDK

4. Create Google Cloud Project and Configure Local Environment

5. Build and Deploy Docker Image with Google Cloud Commands

6. Build and Deploy Docker Image with Docker Commands

7. Create Firewall Rules

8. Deploy Docker Image to Compute Engine and Run

Create Node.js Web App

First, create a simple Node.js “Hello world!” example.

$ mkdir node-web-app
$ cd node-web-app/
$ npm init

after we create package.json

$ npm install -save express

and we add start command at “package.json”, I use vim you can use a simple text editor.

$ vim package.json

Then, We are going to create app.js file that will define a web app using the Express.js framework.

$ touch app.js
$ vim app.js

Then, copy the following code inside app.js

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Our app ready and works locally.

$ npm start

Create a Dockerfile

$ touch Dockerfile

After, we create Dockerfile to copy the following code inside it. If you want a detailed explanation of code check Node.js official “Dockerizing a Node.js web app” post.

FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

Then we create a “.dockerignore” with the following content.

node_modules
npm-debug.log

Install Google Cloud SDK

To install Google Cloud SDK please follow this documentation. After you install it you be able to use gcloud commands.

Create Google Cloud Project and Configure Local Environment

$ gcloud projects create compute-engine-docker-nodejs

It takes a while… After creation, you can see the project.

$ gcloud projects list

Output:

Get the project configuration with the following command if your project name is empty, follow the project id setting step, else skip it.

$ gcloud config list

Output:

If the project name is empty:

$ gcloud config set project <project-id>
$ gcloud config get-value project

Output:

After this point, we will see how we can Build and Deploy Docker Image with Google Cloud Commandsand Build and Deploy Docker Image with Docker Commands

I use both commands depend on my case but if you already use docker, you can continue to use Docker commands.

Build and Deploy Docker Image with Google Cloud Commands

we are going to build and deploy our docker file with the following command.

$ gcloud builds submit --tag gcr.io/<project-id>/docker-image

GCP first upload our folder to GCP Storage Bucket as .tar:

After it gets a Build:

And you can see your image at Container Registry:

If you click it, you will see details and deployment option but for now, we won’t deploy it, there is still some configuration that we must do

Build and Deploy Docker Image with Docker Commands

Step 1) Configured Docker to use gcloud as a credential helper

$gcloud auth configure-docker

Step 2) Build the docker image

docker build -t [HOSTNAME]/[PROJECT-ID]/[IMAGE] .

$docker build -t gcr.io/compute-engine-docker-nodejs/nodejs-local-build .

Notice I use a dot (.) because I am in the same directory with my project. After the build, If you check docker images, you will see our image(nodejs-local-build) and it has a default tag:latest

$docker images -a | grep gcr.io

Step 3) Push docker image to container registry

docker push [HOSTNAME]/[PROJECT-ID]/[IMAGE]

$docker push  gcr.io/compute-engine-docker-nodejs/nodejs-local-build

and our image at container registry.

Create Firewall Rules

We have to make the same configuration if we want our application to communicate outside the world.

Now we can deploy our Image to Compute Engine

Deploy Docker Image to Compute Engine and Run

We use our firewall rule tag to fill Networking tags under Networking

Our deployment is successful.

--

--