How to Dockerize Node JS app?

Step-by-step tutorial to build docker image of NodeJS app

ยท

3 min read

Hello! Everyone,

We'll be learning, how we can dockerize Node JS app in a step-by-step guide.

What is docker & why do we need it?

Docker is a platform for building applications using containers. Containers are lightweight, portable, and self-sufficient.

It helps you to build and run applications entirely inside containers. You can use Docker to run and deploy any application.

Docker simplifies the development cycle by allowing developers to work in standard environments with local containers that supply your applications and services.

Containers are ideal for ongoing integration and delivery (CI/CD) workflows.

Prerequisites

  • Knowledge of Node Js
  • Installation of docker in your local system. You can follow this guide to set it up.
  • Install docker-compose, here's the guide.

Let's get into it!!

Create sample Node JS app

skip this step if you've an existing app

create a directory node-dockerize

run this command

npm init

create a file index.js

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = 'localhost';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});

Create Docker file

create a file called dockerfile in your root directory, add following data into it.

Define node version

FROM node:16

Next create working directory for app inside of image.

# Create app directory
WORKDIR /usr/src/app

Install dependencies, for this we'll first copy package.json from directory to image, then run npm install

#COPY File
COPY package*.json ./

# Install app dependencies
RUN npm install

this will bundle code into image

# Bundle app source
COPY . .

Finally bind app to PORT

EXPOSE 8080

These commands will be used to run app we're using node index.js to run our app, so we'll define this in dockerfile

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

Here's final result of dockerfile

FROM node:16

# Create app directory
WORKDIR /usr/src/app

#COPY File
COPY package*.json ./

# Install app dependencies
RUN npm install


# Bundle app source
COPY . .

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

Now create .dockerignore file

docker images

this will prevent copying of local modules into image

      node_modules
     npm-debug.log

Build Image

Run this command in project's directory to build image

docker build -t docker-image-name/image file name

Example:

$docker build . -t atia/node-dockerize-app

Check if image has been created.

This command will list down all images in your docker container

 $docker images

Run Image

Run this command to start image.

docker run -p HOST PORT:DOCKER PORT -d user-name/docker-image-name

Example:

$docker run -p 49160:8080 -d atiaz/node-dockerize-app

If you want to see the list of images currently running in your system, you can use following command

$docker ps

Hope this tutorial helped you!! if you need any help, you can let me know in comment section.

Thanks for reading! if you want me to cover a topic, feel free to let me know in comments below. ๐Ÿ‘‡๐Ÿ’ฌ

โ˜•๏ธ You may support me by buying me a chocolate: buy me chocolate

Cheers, See you in next one. ๐ŸŽ‰๐Ÿ””

Follow me on instagram, linkedIn, medium, Github๐Ÿ˜ป

ย