Dockerize your Nodejs Application in 5 minutes

Guneet Singh
4 min readFeb 27, 2022

--

Lets start by creating a basic NodeJS application. which we will be Dockerizing. Start by getting into the folder and initializing npm in the folder using

Basic Node application

npm init -y

let’s install express and other required modules and create the server.js file to start with configuring express

npm i express
touch server.js

Now we can start the server to work on port 3000 using the the code provided below

we can also create a simple get route to check if the application is in working state or broken down.

app.get("/",(req,res)=>{
res.send("Hello World!")
})

Dockerizing the application

Start by creating the Dockerfile which instructs the docker with the commands to run while building the image. As we are making the image of the application using the nodejs so the base Image of the application would be nodejs.

FROM node:14

FROM specifies the base image to use to build the image i.e node, the number after colon specifies the version of nodejs. Now we specify the working directory

WORKDIR /app

WORKDIR specifies the location where application is stored. Now we have a location where we can paste the files of our local application to our docker container, for that we will need to copy the files

COPY package.json .

COPY commands copies the file specified(package.json) to the specified location i.e . (current working directory). As we have the dependencies with us in the container we can start installing those dependencies using

RUN npm install

RUN as its name suggest would run the command inside the working directory. This would download all the dependencies required to run the application.

Time to copy rest of the code to the docker container try to guess the command

COPY . .

Woo-ooh, you got that right, if you find this article helpful make sure to follow subscribe and Give a heart.

COPY will copy all the files in the local working directory to the container working directory.

Now we need to expose the port our application is working on so that it can listen to all the incoming traffic using

EXPOSE 3000

3000 is the port number i will be running my application on, you can change the port accordingly. Now since everything has been brought to our docker container. Time to start our application using

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

CMD specifies the command to run the application.Now our Dockerfile looks like this

Make sure to start the Docker Desktop otherwise you might face errors.

Docker Commands

Now it’s time to try the docker commands, first of all we will need to build the image using the Dockerfile. You can use the command

docker build --tag node-docker .

docker build is used to build an image — tag specifies the Name and version of the docker image i.e node-docker, we need to tell the Docker where the Dockerfile is kept for it to build the image i.e . (or the current working dir)

To check if the image is successfully build we can use

docker image ls

it list down all the images that are present in the docker. You will find an image named node-docker . It’s time to start the container from the image using

docker run -d -p 3000:3000 --name node-container node-docker

docker run starts a docker container which it is going to build from the image node-docker and the name of the container is node-container, -d tells the container to run in detach mode and -p specifies the port to which it should direct the incoming traffic to.

We successfully dockerized the application within few minutes.

You will find that you wont see any changes if you make any code change, its because the docker image we builded is from the last code change so we will need to do the entire process of

  1. Building an image
  2. Starting the container

which makes it a tedious process and not a way anyone would prefer to use, Don’t worry it was just a way to dockerize your application in minutes.

I will get the development setup 👨🏼‍💻 of docker covered in another post ✍🏼 so make sure to follow me and subscribe to the newsletter❤️ so you don’t miss any update.

You will also learn how to use multiple container and how to handle multiple container using docker-compose and many more stuff

--

--

Guneet Singh
Guneet Singh

Written by Guneet Singh

I am a software developer also a student, Helping other developers in there development journey. You can help me by following me here.

No responses yet