How to Build a Node.js Application on Docker

Filed Under: Node.js

Docker has been the latest trending topic over the past couple of years and if you haven’t been to space or spent some time in the caves, you probably must have come across this tech buzzword.

So, what is docker?

Docker is a free and open source development platform that allows developers to develop and deploy their applications in containers. Containers come with their own libraries and frameworks and enable applications to be developed in an isolated environment from the host Operating System. This guide takes you the process of how to build a Node.js application on Docker.

So why should you build your application on docker?

  1. The consistency of the working environment. The application will run in the same manner for all developers and clients alike at whatever phase; be it in development, staging or in production.
  2. Docker containers ship their own libraries and frameworks as earlier pointed out. Containerization eliminates the need for manually installing libraries and dependencies. You can launch a fully fledged and complete development environment ready for deployment of your application.

Now let’s dive in and see how you can build a Node.js application on Docker.

Prerequisites

  • Docker installed on your system
  • NPM

Step 1: Creating the Node.js application

The first step is to create a Node.JS application
Create a new directory and navigate into it

$ mkdir nodejs-app && cd nodejs-app

Next, initialize the directory with npm (NodeJS Package Manager). This creates a package.json file that will handle dependencies of the application.

$ npm init

Sample Output

Initializing npm on Ubuntu 18

You will be prompted for the details of the application such as Name, Version, description, etc. You can decide to specify your won preferred details of if you desire to leave the defaults as they are, simply press ENTER.

Json Package Details on Ubuntu 18.04

At the end of the prompt, type Yes to accept the changes.

Next, run

 $ npm install express –save 

Sample Output

Npm Install Express Save command on Ubuntu 18.04

This adds the express framework as the first dependency

Step 2. Creating files for the Node.js application

We are going to create 3 files for the Node.Js application.

  • index.js
  • index.html
  • about.html

For the index.js file, add the following content.


const express = require('express')
const app = express()
app.get('/', ( req, res ) => {
    res.sendFile(`${__dirname}/index.html`)
})

app.get('/about', (req, res) => {
    res.sendFile(`${__dirname}/about.html`)
})
app.listen(3000, () => {
    console.log('Listening on port 3000!')
})

The above file creates 2 routes for the index.html and about.html pages which listen on port 3000.

For the index.html file, add some test content as shown.

Sample Index Html File

For the about.html page, add the following content.

About Page Sample File

Great! Our Node.JS application is now ready for launching.

Step 3: Running the Node.JS application

To run the application, run the command

$ node index.js

Sample Output

Node Index Js Running On Port 3000

Now, open your browser and go to index.html page by typing the URL as shown

http://ip-address:3000

Index Html Page On Port 3000

To visit the about.html page, append the /about at the end of the URL

http://ip-address:3000/about

Step 4: Dockerizing the Node.JS application

Next, you are going to define a working environment inside your container. This working environment encompasses dependencies, pre-installed applications, and libraries to mention just a few. The container doesn’t come with the whole Operating System, as opposed to a VM. It only ships dependencies, applications and config files and this makes it much lighter, faster, and resource friendly than ordinary virtual machines.

Contrary to VM, however, the container doesn’t hold the whole operating system—just applications, dependencies, and configuration. This makes Docker containers much lighter and faster than regular VM’s.
Create a file dockerfile in the app’s directory

$ touch dockerfile

Add the following content


FROM node:carbon

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

The first line indicates that we went to use the latest node version for building our image
The second line creates the directory /usr/src/app for holding the app’s code inside the image.
The third and fourth line copies the package.json, file and runs the npm install command

The COPY instruction bundles our app inside the Docker image, so our HTML files and index.js file in our case.
The EXPOSE instruction exposes the 3000 port that our app uses.
Finally, the CMD instruction specifies the command that needs to be run for our app to start.

Step 5. Building the Node.JS application

With the dockerfile now created, let’s build our app using the command. Please note the presence of space and period sign at the end.

$ docker build -t node-app .

Sample Output
Build Node App on Ubuntu 18.04

The -t enables you to tag your docker image. In this example, we have tagged our docker image as node-app

To verify the existence of the application run

$ docker images 

Sample Output

docker Images command

Step 6. Run docker container

We can now launch our container

# docker run -p 8080:3000 -d node-app

This redirects the app to open via port 8080 instead of port 3000.

Sample Output

Node App Running On 8080

Step 7. Pushing the App to Docker hub

The final step will be to share the node application to docker hub where people can access it and download its image.
To do so, ensure you have an account at https://hub.docker.com
Next, build the image again using your docker hub credentials as shown

# docker build -t [USERNAME]/tag .

In our case, the command will be

# docker build -t winnieondara/node-app .

Where username => winnieondara
Image tag => node-app

Build Node App on Ubuntu 18.04

Next, login to docker on the terminal using your credentials

# docker login

Sample Output

docker Login command

Finally, push the image to docker hub

# docker push winnieondara/node-app

Sample Output

Push Image To Docker Hub

Congratulations! Now, log in to your docker account and be sure to find your Node.JS application image as shown. The image can then be downloaded and run by other users

build a Node.JS application on docker

This wraps up our tutorial on how to build a Node.js application on docker. Feel free to give it a try and tell us how it went. We value your feedback.

Comments

  1. rajani says:

    Great Article !! Thanks for sharing..

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages