Dockerizing and Deploying a NodeJs App

Collins Delan
Jul 25, 2017 · 5 min read

Today we are going to write a simple NodeJs app, dockerize and deploy it to Google cloud using Kubernetes. The main objective of this article is to give an insight of deploying a NodeJs app to Google cloud. Therefore, no Javascript experience is required. I’ll assume you have never worked with JS. The code base is going to be very basic. Let’s do this!

Github repository here.

Section A: Node App

  1. Open your terminal. Create a folder with any name. I will call mine nodeapp, i.e mkdir nodeapp.
  2. Go to the created directory. Run npm init. Follow the steps until the end. After this you’ll have a package.json file in the root of your folder.
    Modify this file to have same dependencies as in this file.
  3. Now let’s create two files. One a server side file and the other client side. For the server side, let’s call it server.js and the other call it index.html . Our server should have the same code as in this file. You are free to put anything in your index.html file. Here is mine.
  4. Before testing our application, we have to install the dependencies. Run npm install or yarn install . This will generate a folder called node_moduleswhich houses the packages. After this process is successful, run npm start . Go to your terminal and you should see something similar to App Listening at http://localhost:3000 . Visit the link on your favorite browser. You should be able to serve the contents of your client file. Now we have our app ready!
Figure 1: This is my simple client side

Section B: Dockerizing The App

Hope everything is going on well so far. Please make sure you have the app we created in section A. Now let’s dive into something interesting, containerisation. Here we’ll use Docker. Let’s install it first. Installation varies with OS. Here is the link to guide you installing docker on your machine. To confirm if it was successfully installed, run docker version or docker info .If all is good, let’s continue…

Create a file in your root directory and name it Dockerfile . Please note it doesn’t have any extension. It will contain various commands and arguments to automatically perform actions on a base image in order to create a new one. It will define what goes on in the environment inside the container. Read more here. Update your Dockerfile with the following:

FROM nodeENV NPM_CONFIG_LOGLEVEL warnRUN mkdir -p /usr/src/appEXPOSE 3000WORKDIR /usr/src/appADD package.json /usr/src/app/RUN npm install --productionADD . /usr/src/app/ENTRYPOINT ["npm", "start"]

Now, let’s build the image. Run docker build -t nodeapp . Don’t forget to include the period. You can tag your image with any name. Mine I have tagged it, nodeapp as shown in the command.

Sample of my build logs.

Run docker run -p 3000:3000 nodeapp . On your browser, go to localhost:3000. You should be able view your application! Your app is now dockerized! To view your image, run docker images . This will display images with their details. To run your app in the background(detached mode), run docker run -d -p 3000:3000 nodeapp . Run docker ps to view your container, docker stop <CONTAINER ID> to stop the container.

Section C: Deploying to Google cloud

We want now to make our application public.

First off, you need to have google cloud platform account for hands-on practice. Please sign up here. The good news is that, you’ll get $300 in credit and 12 months to explore all of the Google Cloud Platform. This is awesome, right?. Let’s do this!

Google have a good installation guide here. This will guide you from the requirements to setting up gcloud in your local machine. It should take less than 10 minutes depending on your internet speed.

Assuming that you now have gcloud set, let’s move on. Login to youe google cloud platform console. Create a project. Give it any name. Take note of the project id. Here is a guide on how to create and manage a project.

Pushing The Docker Image

Note: Before you can push a Docker image to any private registry, you need to tag the image with a registry name. According to the docs, your registry name will be the project id combined with host name. The valid registry name format is:
<HOSTNAME>/<YOUR-PROJECT-ID>/<IMAGE-NAME> for example us.gcr.io/nodeapp-123456/nodeapp . Replace [YOUR-PROJECT-ID] and [IMAGE-NAME] with your valid project id and image name respectively. Clear? Let’s now build another image using this format(Note: This is what you’ll push). Run docker build -t <HOSTNAME>/<YOUR-PROJECT-ID>/<IMAGE-NAME> . . Confirm that your image has been created.

Push your image by running this command:

gcloud docker -- push <HOSTNAME>/<YOUR-PROJECT-ID/<IMAGE-NAME> , for example, gcloud docker -- push us.gcr.io/nodeapp-123456/nodeapp .

Next we need to configure Kubernetes. First, install kubectl using the following command, gcloud components install kubectl . Verify it is installed via gcloud components list . Next create a cluster. Please follow any of the method described here. It’s straight forward. Okay, let’s configure kubectl with the created cluster. Run the following commands one by one:

gcloud config set project <YOUR-PROJECT-ID>
gcloud config set container/cluster <CLUSTER-NAME>
gcloud config set compute/zone <ZONE-NAME>
gcloud container clusters get-credentials <CLUSTER-NAME>

Note: Ensure kubectl has the right credentials by running
gcloud auth application-default login
.

Awyeah! Let us now deploy our app. Run the following command:

kubectl run <DEPLOYMENT-NAME> --image=<HOSTNAME>/<PROJECT_ID>/<IMAGE-NAME> --port=3000

Make your app public by using this command:

kubectl expose deployment <DEPLOYMENT-NAME> --type="LoadBalancer"

Check the status of your deployment. If all is good, the status on your pod should be, Running .

How will you access your remote app? How will you share a link to allow other people access your app? Don’t worry. Run kubectl get services . Use the EXTERNAL-IP to access the app. Don’t forget include the port. For example, 30.123.119.166:3000 . You should now access your app via the address.

It has been a long journey, right? Let me say congratulations for building, dockerizing and deploying an app. Even though it was a very simple app. I believe you’ve learned something.

Note: I have not covered a lot of stuff here. Please use gcloud, docker and kubernetes docs to learn more.

Credits:
- Google Cloud Platform docs
- Docker docs

Your comments are highly welcomed!

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