Cloud computing and docker make web application deployment easy

Developing a web application, either a website or web API, is becoming more and more easy nowadays. There are plenty of choices in terms of languages, PHP, Python, Ruby, Java, C#, Go, JavaScript(Node.js) etc. However, once the development is done on the local machine, deployment to servers could be a painful process. Making sure dependencies are working, configuring web servers, getting the https certificates…… Thanks to cloud computing technology and docker, this process could be simplified to a level that people just need to focus on developing the web application logic.
In this article, we will walk through how to deploy a simple Node.js web application using GCP(Google Cloud Platform) and Docker.
1. Create a simple Node.js Express web application
const express = require("express")
const app = express()const index = function (req, res) {
const date = new Date()
res.send(date.toISOString())
}
app.get("/", index)const port = 3000
app.listen(port, () => console.log(`app listening at ${port}`))
Above is a very simple web application, the root route will return the current timestamp. Next step, we will dockerize this application so that it can be deployed to any environment with Docker installed.
FROM node:alpineWORKDIR /usr/src/appCOPY . /usr/src/appRUN npm installEXPOSE 3000CMD ["node", "index.js"]
Above is the Dockerfile with which we will make the web application to be a docker image. If everything works fine, the folder looks like:

.dockerignore file contains the files or file types that you do not want to copy to the dockerized environment. In this case, it includes “node_modules”, “npm-debug.log” and “package-log.json”.
Now, we can build the docker image via the following command:
docker build -t sample-web .
Above command will build a docker image called “sample-web”
2. Push the image to GCP(Google Cloud Platform)
Make sure you have installed the Google Cloud SDK already. Run the following commands to push the docker image we just built to GCP Container Registry, so that the GCP can use the image to make deployment to GCE(Compute Engines). GCP can NOT use docker hub images to make deployments.
// make sure we login first
gcloud auth login// make sure Google Cloud SDK can access the local docker images
gcloud auth configure-docker// make tag to the docker images we just built
docker tag sample-web [HOSTNAME]/[GCP PROJECT ID]/[IMAGE]// push the tagged image to google Container Registry
docker push [HOSTNAME]/[GCP PROJECT ID]/[IMAGE]
[HOSTNAME] is one of gcr.io, us.gcr.io, eu.gcr.io, asia.gcr.io; IMAGE is the image name you want to have, in this case, can be “sample-web”.
3. Deployment in GCP using “Instance Group” and “Load Balancer”
First, we create a instance template, go to “Compute Engine” -> “Instance templates” -> Click “create an instance template”.

Make sure we click the Container checkbox and input the image route [HOSTNAME]/[GCP PROJECT ID]/[IMAGE]
In the networking setting section, set the “External IP” to be “None”, we will use “Load Balancer”, so the Compute Engine will not be directly reached from the internet, it is also safer this way. Also, in “Network tags”, input “sample-web-port”, it is a firewall rule, we will create later.
Then, we click “Create”, we will have our template ready.
Now, we create the firewall rule, go to “Network services” -> “Firewall rules” -> Click “Create firewall rule”, input the name you prefer, in the Target tags, input “sample-web-port”, which is the value we used in the instance create section. In the “Source IP ranges”, input “35.191.0.0/16” and “130.211.0.0/22”, these 2 values are pre-defined by GCP, they are used to check the health of instance. In the “Protocols and ports”, choose “tcp” and input “3000”, which is the port we set in the Node.js web application.
Then, we click “Create”, we will have firewall rule ready.
Second, set up Cloud NAT . Go to “Network services” -> “Cloud NAT” -> Click “Create NAT gateway”. In the Region, select the Region you like(it should be the same as the instance group we are about to create later). For “Cloud Router”, just create a new one.
Then, we click “Create”, we will have NAT gateway ready, it will make sure the internet health check we define later can be done properly.
Third, we create the “Instance group”. Go to “Compute Engine” -> “Instance Groups” -> Click “Create instance group”, input the name you prefer, choose the Region and Zone you like, in the “Instance template”, choose the template we just created. In “Port name mapping”, input “http”(any name you want), then port numbers to be “3000”(to be used later in load balancer). For auto-scaling setting, you can configure according to your needs, since this is just a simple demo, we set the “Maximum number of instances” to be 2. In “Health check”, click “create health check”, make sure the protocol is “HTTP” and port is “3000”, then click “Save and continue”.
Then, we click “Create”, we will have our instance group ready as follows:

We named our health check “hc-3000”, in location you should see the region and zone you selected for this instance group.
Last, we set up our “Load Balancer”. Go to “Network services” -> “Load balancing”, Click “Create a load balancer”, choose “HTTP(S) Load Balancing”. Choose “From internet to my VMs”. In “Create back-end service”, choose the instance group we just created, and the named port we just created as well. For health check, use the same one we used in the instance group.
then last step is the “Front end configuration”, this one comes useful since it can configure HTTP and HTTPS, in IP address, we click “Create IP address”, this one assign a static IP for our balancer. For HTTPS, you can create a new certificate, this is the greatest part, since configure HTTPS certificate using nginx in a compute engine is a really painful process. Here, you just need to choose “Create Google-managed certificate” and enter then domain names.(we have to configure your domain name DNS to point to the static IP we just created, you can get free domain names from a lot of providers).
Then we click the last “Create”, we will have our “Load Balancer” ready.

4. Now check it out!
We can now go to the HTTPS web address we configured for our web application. We shall see something like:

It works!!!
Although the whole process might seem consist of several steps, but all of them just simple click buttons and input strings. Once you have gone through the process once and understand some basic logic of cloud computing, you will find this process is way easier than setting up all the configuration of your own web server.
Hope you find this article useful, thank you all.