Dockerize NodeJs ExpressApplication

Abhay Ahire
3 min readJan 3, 2022

In this article, we’ll see how to dockerize a simple nodejs application.
First we’ll create a simple nodejs express server and then create docker image for it and run that docker image.
So let’s start with creating basic nodejs express server.
To start with create a directory in your computer where we’ll add all files.
Inside the directory use following command to initialise nodejs project

npm init -y

-y flag instructs node package manager to take all default options while creating package.json

Let’s add express package using following command

npm install express

Now let’s create node server using express as follows

And here we have running nodejs server, you can headover to browser and type ‘http://localhost:3030/’ and get response.
Now let’s start dockerizing our app, before that make sure to install docker on your system.
First of all, we’ll create Dockerfile to instruct docker what to do.
So create file name Dockerfile in your directory and add following contents in it. Name should be exact match as above.

.

Let’s see what we are doing here.
To run our nodejs app inside docker, we’ll nodejs in our container. This application is really small and just for demonstration purpose, so we are using alpine image which is really small and good fit for this type of application.
To tell docker to use alpine image, we have added

FROM node:10-alpine

On the next line, we have created work directory for our docker container where all source files be stored.
Now we need to copy our package.json into our working directory which holds all meta data about our application, we do that by

COPY package*.json /app/

Now after adding package.json we’re doing npm install to get all needed packages. After we are copying all our source from our directory to newly created work directory using

COPY . / app/

Now we instruct container to run our express server, I have specified two ways to write commands, you can use anyone

CMD node index.js

Now finally we need to expose a port which later we’ll be mapping to our systems port so that we can access our running app through that port.

EXPOSE 3030

And we are done, now we’ll try to create a docker image and run it.
Use following command to create docker image.

docker build . -t aaa-host/node-docker-app

docker build is used to build docker image, period (.) is specified where to locate dockerfile. -t flag is used to add tag to created image. Here we added tag as ‘aaa-host(username)/node-docker-app(app name)’
It first pulls node images from docker hub if not present in your system.
Once image is successfully created, you can see that image using command.

docker images

Now, we’ll run docker image using

docker run -p 3000:3030 aaa-host/node-docker-app

Here -p flag is used to map dockers 3030 port to our system port 3000 (This can be any port that’s not being used in your system)
And that’s it, head over to browser but this time type ‘http://localhost:3000/’ and you’ll see server responding back.
To clone the ready code base from git head over to https://github.com/abhay-321/dockerize-node-app.
Thanks for reading, In case of any doubts feel free to ask them in comment section, I’ll be happy to help 🙂

--

--

Abhay Ahire

Full stack Dev with Angular, Java , Node and Devops Engg AWS