npm init

Dockerize nodejs web application step by step guide

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
npm init
npm init
  • 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 helps npm 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 folder nodedocker
  • 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:
node js app
  • 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 name Dockerfile. This file holds the instructions that defines a docker image. Docker builds image automatically based on the instructions written in Dockerfile.
  • 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 the latest version of node available in docker hub. So write the following command as the first line of Dockerfile.
  • In the second line of Dockerfile, we’ll specify a work directory that will hold application code inside the image:
  • In the third line of the Dockerfile, we’ll copy the the dependency holder file package.json
  • In the fourth line of the Dockerfile, we’ll install npm
  • In the fifth line of the Dockerfile, we’ll use COPY instruction to bundle application source code to inside the docker image
  • In the sixth line of the Dockerfile, we’ll use EXPOSE instruction to tell docker that the service running in the container can be connected on port 8080. If you recall, this is the port we mentioned in our index.js file.
  • In the last line, we will instruct Docker to execute the node index.js command to run our application
  • Now, if we put all of the above, our docker file will look like below

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
  • So, using the command terminal, let’s traverse to the Dockerfile location (which is D:\nodedocker in my case) and execute following command
  • 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
docker image
docker images
  • 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
  • Now, let’s go to browser and hit http://localhost:8081. We’ll see the same output; however, this time from the container.
rendering nodejs web app from docker 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!

94

No Responses

Write a response

This site uses Akismet to reduce spam. Learn how your comment data is processed.