In the modern software development process, Docker and Node.js are the ever trending technologies now. Application containers have emerged as a powerful tool in modern software development. New docker container concept is lighter and more resource efficient than traditional virtual machines. Its helping IT organizations new opportunities in version control, deployment, scaling, and security.

In this article we’ll show you how to create a simple node.Js app and create a docker image of the node.js application. Containerizing your Node.js applications with Docker Dockerize your Node.js application, go throuh each steps below.

Why you should use docker and containers

Docker is a powerful tool for creating and deploying applications. it is a container management service. The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere. dockerize node.js application.

In Docker, everything is based on Images. An image is a combination of a file system and parameters.

A containers are running instances of container images. Rather than virtualizing the hardware to run multiple operating systems, containers rely on virtualizing the operating system to run multiple applications. This means you can run more containers on the same hardware than VMs because you only have one copy of the OS running, and you do not need to preallocate the memory and CPU cores for each instance of your app.

Just like any other app, when a container needs the CPU or Memory, it allocates them, and then frees them up when done, allowing other apps to use those same limited resources later.

image source: docker.com

Ok lets create our node js basic application. What do you need to install on your computer?

Install Below Softwares:

Node.js : This is required.

NPM : This is required, install latest npm.

Express: We will use most popular Express Framework in our application, so install it.

Step1:

Create a directory

$ mkdir docker-nodejs-app-deploy

Change directory

$ cd docker-nodejs-app-deploy

Initialize NPM

$ npm init
PS D:\infotechseo\docker-nodejs-app-deploy> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (docker-nodejs-app-deploy)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository: (https://github.com/infotechseo/docker-nodejs-app-deploy.git)
keywords:
author:
license: (ISC)

npm init will ask you some details, just fill it or just enter without any input. I have skip all options. A package.json file will be created at root.

now install express

$ npm install express --save

Final package.json

{
  "name": "docker-nodejs-app-deploy",
  "version": "1.0.0",
  "description": "Dockerize a Node.js application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/infotechseo/docker-nodejs-app-deploy.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/infotechseo/docker-nodejs-app-deploy/issues"
  },
  "homepage": "https://github.com/infotechseo/docker-nodejs-app-deploy#readme",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Now create a index.js file in root folder, that will serve as entry file. Open it and insert the code as below.  A simple HTTP server that will serve our Hello World website:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})


app.listen(4001, function () {
  console.log('app listening on port 4001')
})

Run the App

$ node index.js

Open http://localhost:4001/ in your browser to view it. you can see the ‘Hello World!

Step2: Dockerizing Node.js application

Install Docker : Use below link to install docker as per your operating system.

Create Dockerfile

To dockerize this application we need to create a Dockerfile in the application root. The Docker image is created with instructions written in the Dockerfile. Make sure there is no extension in Dockerfile. The whole Dockerfile should look like this. dockerize node app production

FROM node:latest
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.js
EXPOSE 4001

Explain the lines:

FROM node:latest – Use the latest node.js version.

WORKDIR /app Set working directory in the container to /app. Here in this directory to store files, run npm, and launch our application.

COPY . /app – copy application files and folders including package.json in the app directory.

COPY package.json /app

RUN npm install – It will check package.json and install all dependencies of the application. Installation of the packages is onetime for the single image. Docker do not need to install the packages for containers.

CMD node index.js – Once installation is complete. This line tells about the entry point of the application what need to be run in order to run the application.

EXPOSE 4001 – Expose port 4001 to the outside once the container has launched.

Build docker image

docker build command is used to create docker image.

$ docker build -t hello-world .

Run docker container

docker run command is used to run a container

docker run -p 4001:4001 hello-world

here docker ps command is used to check the container. One container is created here having conainer ID, Image details.

Now our conatiner is working. Open the browser again and try http://localhost:4001/

Congratulations website is working but this time its a running container.

You can find the above code at github https://github.com/infotechseo/docker-nodejs-app-deploy

Please comment if anything is missing or you want extra. Thanks for reading article on dockerizing a Node.js web app, Build A Production Ready Node/Express API With Docker.

Advance : Read Build a highly available Node.js application using Docker node js interview

Similar Posts