In today’s time of cloud computing, containerized applications are more relevant than ever. Docker is de facto standard for building and sharing containerized applications. In my previous post, I demonstrated how to build a simple hello world application using nodejs. Now, I’ll write a step by step guide to dockerize a similar nodejs web application.
My assumption is that you have basic knowledge of docker and have the docker installed in your system. Even though, I will demonstrate using Windows, it is similar for users with Linux or mac systems.
I’ll divide the article in 2 parts. In the first part, we’ll create one nodejs web application. In the second part, we’ll dockerize the same.
Create node.js web application
- We’ll first launch a command window. I’ll use gitbash command window. But you can also use usual windows command window.
- Now, I’ll traverse to D: drive and create a folder for this project with name “nodedocker”
- Then in the command prompt, we’ll execute a command
npm init
- When executed, above command will ask for details of the application. If you are happy with the default values, just press
Enter
key. If you would like to change any value, key in your preferred details. For reference, take a look at the below screengrab of my application

- This wizard basically creates a
package.json
file inside the current directory. This file holds various metadata relevant to the project. This file also contains information that helpsnpm
to identify the project. It also handles dependencies of the project. - We’ll now write a very simple script that will create a web server and when executed, render Hello World! message.
- Copy the below piece of code and save in a file with name
index.js
inside the above created foldernodedocker
1 2 3 4 5 6 7 8 9 10 |
const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World!'); }); server.listen(8080, function() { console.log('Server running on port 8080'); }); |
- Now, go back to command prompt, traverse to
nodedocker
folder and issue this command:node index.js
- Then, open a browser and hit http://localhost:8080. You will see something like below:

- So, with this, we are now done with the first part of our activity i.e. to create a sample nodejs web application.
Dockerize nodejs application
Now, we’ll learn how to dockerize the above created nodejs web application and run it as docker container.
- Go back to
nodedocker
folder and create an empty file with nameDockerfile
. This file holds the instructions that defines a docker image. Docker builds image automatically based on the instructions written inDockerfile
. - The first line in the
Dockerfile
will tell Docker as to what will be our base image from which we want to build our own image. In this case, we will use thelatest
version ofnode
available in docker hub. So write the following command as the first line ofDockerfile
.
1 |
FROM node:latest |
- In the second line of
Dockerfile
, we’ll specify a work directory that will hold application code inside the image:
1 2 |
# Create work directory that will contain source code and other details WORKDIR /usr/src/app |
- In the third line of the
Dockerfile
, we’ll copy the the dependency holder file package.json
1 |
COPY package.json ./ |
- In the fourth line of the
Dockerfile
, we’ll install npm
1 2 |
RUN npm install |
- In the fifth line of the
Dockerfile
, we’ll useCOPY
instruction to bundle application source code to inside the docker image
1 2 3 |
# Bundle application source code COPY . . |
- In the sixth line of the
Dockerfile
, we’ll useEXPOSE
instruction to tell docker that the service running in the container can be connected on port8080
. If you recall, this is the port we mentioned in ourindex.js
file.
1 |
EXPOSE 8080 |
- In the last line, we will instruct Docker to execute the
node index.js
command to run our application
1 |
CMD ["node", "index.js"] |
- Now, if we put all of the above, our docker file will look like below
1 2 3 4 5 6 7 8 9 |
FROM node:latest # Create work directory that will contain source code and other details WORKDIR /usr/src/app COPY package.json ./ RUN npm install # Bundle application source code COPY . . EXPOSE 8080 CMD ["node", "index.js"] |
Build the docker image
- Now, we are ready to build the docker image with the instructions we have just put in the above
Dockerfile
. - To build the docker image, we need to run a command like below
1 |
docker build [username]/[tag] [Dockerfile location] |
- So, using the command terminal, let’s traverse to the Dockerfile location (which is
D:\nodedocker
in my case) and execute following command
1 |
docker build -t mohammed/nodedockerapp:latest . |
- It’ll now execute all the instruction one after another and build an image. Once completed, you can issue
docker images
command to see the newly built image

- With this we have successfully built the docker image. Next, we will create a container from this image.
Run the docker container
- Now that the image is built up, let’s launch the container. One important point to note here is that the port 8080 that we have used in our code and also exposed in the image will not be visible outside of the container. Hence, we’ll need map the container internal port 8080 to an external port (on host machine). Let’s use an external port 8081. While launching the container, we will use following parameter:
-p [external port]:[internal port]
- So, without further delay, let’s execute following command
1 |
docker run -p 8081:8080 mohammed/nodedockerapp |
- Now, let’s go to browser and hit http://localhost:8081. We’ll see the same output; however, this time from the container.

Conclusion
This is the end of this blog where we learnt how to dockerize a simple nodejs application in step by step guide. Happy learning!