A Guide to Dockerize your Node.js Application

Sharmila S
featurepreneur
Published in
4 min readMay 29, 2021

In this article, we are going to learn to dockerize a node.js application.

Prerequisites

  • Node
  • npm
  • Docker

1. Create a node.js app

First, let’s create a simple node.js(express) application, to get started with express, read this article.

Create a file named index.js which is our express app, and add the following.

const express = require("express");const app = express();
const PORT = 4000;
app.get('/', (req, res) => {
res.send("Hello World"
});
app.listen( PORT, () => console.log("Server is listening to port" + PORT ));

Now, let's try to run our app in the terminal,

$ npm init$ npm install express$ node index.js

Our app is running now, to check, go to http://localhost:4000

App running at port 4000

2. Set up the Dockerfile

The next step is to create a Dockerfile.

If you haven’t installed docker in your system, visit here to download it.

We will always start from a base image, and build our own image with our app in it.

For the base image, we are going to use the official node image available in dockerhub. (Dockerhub — where users can create their own private/public repositories to contain their images and also can access any open-source image). For more details about the node image, click here.

node image in dockerhub

Before moving on, there are a couple of things to look at.

  • Variant — They are several variants available for node image, we are using the alpine variant due to its small size.
  • Version — You can choose the version of node you are using. I'm using 14.17.0 version. You can check the node version in your system by typing the following in your terminal.
$ node --version

Now, let’s create our Dockerfile. ( Dockerfile — contains all the commands that are to be executed, to build an image).

Create a file named Dockerfile (with no extension). And add the following.

FROM node:14.17.0-alpine
WORKDIR /app
ADD package*.json ./
RUN npm install
ADD index.js ./
CMD [ "node", "index.js"]

FROM node:14.17.0-alpine —From the base image node, with the version 14.17.0 and alpine variant.

WORKDIR — We are mentioning that the directory called app is going to hold our project. If the directory specified doesn’t exist in the image, it is newly created.

ADD package*.json ./We are adding our package.json and package-lock.json to our workdir.

RUN npm installThis command installs the dependencies mentioned in our package.json

ADD index.js ./— Adds index.js from our app to the image.

CMD [ "node", "index.js"]Executes node index.js , which is the command to run our app

3. Build the image using Dockerfile

Being in your project directory, run the following command.

$ docker build -t docker-express-app .

. — checks for the Dockerfile in the current directory, using which the image is built.

-t docker-express-app — represents the name for the image along with its tag, here, we leave the tag to be default (latest). (-t option is used to specify a name and a tag for the image to be built)

To view all images in your system, type

$ docker images

4. Create an instance of the image (container)

We created the image, now let’s run our image.

$ docker run --name express-api -d -p 4000:4000 docker-express-app

--name express-api— we are naming our container with --name option, express-api is the name given.

-d — to run our app in detached mode.

-p 4000:4000-p option is to map a port to our image, here we are mapping the port 4000 of our localhost to the port 4000 of our container.

docker-express-app — the image to run

Mapping port 4000 localhost to 4000 container express-api

Now, we can view our running instance using,

$ docker ps

We can see our app running, go to localhost to port 4000 to view our app.

App running

To stop/start the instance,

$ docker stop express-api$ docker start express-api

express-api is the name of the instance(container)

--

--

Sharmila S
featurepreneur

Software Engineer | Writes about Full Stack Web Development | 1 X AWS | CKA http://sharmilas.bio.link/