Blog Details

Dockerfile for NodeJS Application

+1
0
+1
0
+1
0
+1
0
+1
0

What is Docker?

Docker is a set of tools that helps to achieve OS level virtualization. Using docker, we can run multiple instances of the same application by making use of containers. Each containers has its own set of dependencies and base image and the required files defined using a definition file called dockerfile.

Simply, think of it as ships that transfer containers to different regions through sea. Take the sea as your server or your system OS, the ships as the cluster of containers, each of the ships have a number of containers that it is transferring and is protected from floating freely in the sea using the ship. Each of the containers will have a number of contents which could be similar in behavior. Similarly, in the software world as well, each of the containers have its own environment and purpose which is separated virtually from the OS and at the same time the OS act as the baseline for running the Sevices and providing a base platform with resources as we define.

Why not Virtual Machine?

The main drawback of Virtual Machine is that it could get heave and could eat a lot resources. It is installing a different OS inside and OS for which we have to allocate set of large memory and other resources. The install OS inside another OS and use it as base of running all the services while containers use the host OS as base service provider and runs in it which result in reduced resource usage and light weight process.

Let’s Start

A dockerfile defines what are the dependencies, base image and other configurations used to build a container. So the main concept in simple is to define the dependencies, what ports need to be opened to public, how to run the process/application when the container starts up. Below is the DOckerfiel definition that I use for a simple NodeJS express application

FROM node:10.22.1-alpine


WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY ./src ./src

RUN npm run build

EXPOSE 3001

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

Explanation

FROM node:10.22.1-alpine

This line defines what base image to be used. You can find various base images in https://hub.docker.com/. Try to use the lightest image to reduce the size and overhead of your container.

WORKDIR /usr/src/app

Setting the working directory for which the commands we’re running to be executed

COPY package*.json ./

Copy package.json and package-lock.json to the working directory.

RUN npm install

Installing dependencies from package.json for the application

COPY ./src ./src

Copying your source code into your container

RUN npm run build

Building the NodeJS application in container

EXPOSE 3001

This tells the container that port 3002 has to be open to public since my application uses port 3002 to deliver the content/API

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

CMD is to run a command-line instruction. Here it tells the container to run node server.js once all the setup is complete.