Apps Development, Cloud Computing, DevOps

4 Mins Read

Dockerizing NodeJS Applications for Seamless Deployment

Introduction

In today’s fast-paced software development landscape, efficient and streamlined deployment processes are key to successful application delivery. Dockerization has emerged as a popular solution for packaging applications into portable and self-contained containers, providing a consistent environment across different platforms and ensuring smooth deployment across various infrastructure setups.

Node.js, a widely used JavaScript runtime, can greatly benefit from Dockerization. By containerizing Node.js applications, developers can achieve scalability, portability, and easy management while maintaining a high level of consistency and reliability.

In this blog, we will explore the concept of Dockerization in the context of Node.js applications. We will delve into the advantages of using Docker for Node.js development, highlight the key components and terminology associated with Docker, and guide you through the process of Dockerizing your Node.js application step by step.

Docker

Docker is an open-source platform that enables developers to create, deploy, and run applications in containers. Containers are a lightweight way of packaging software with all the necessary dependencies and configurations to ensure the application runs consistently across different environments.

Docker allows developers to build, ship, and run applications across different platforms, including laptops, servers, and cloud platforms. Docker provides a way to standardize application development and deployment processes, reducing the risk of errors caused by differences in operating systems and environments.

Docker consists of two main components, the Docker engine, and the Docker client. The Docker engine is a lightweight runtime environment that runs the containers, while the Docker client provides a command-line interface and API for managing the containers.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Step-by-Step Guide

Step 1: Creating the NodeJS App

Note: In this application, I will be using VS Code Editor. You can choose the IDE or Code Editor of your choice.

Create your project directory, open the terminal, and navigate to the project directory. In your terminal, give the command npm init -y. It will generate a boilerplate for our node application. It should look like this –

Step 2: Next, we will install Express.JS using the command npm i express in our terminal. It will install Express in our application.

Step 3: Now, create an index.js file with a simple GET endpoint using the ExpressJS framework. The code for this is –

Step 4: Creating Docker File

Now create a DockerFile in your root directory. A DockerFile is a script specifying the application’s environment and dependencies.

The first step in the DockerFile is to specify the base image, which will be used as the starting point for building the container. For a Node.js application, you can use the official Node.js Docker image. Specify the version of Node.js you want to use as the base image in the DockerFile.

FROM node:16-alpine

Next, we create a directory to hold the application code inside the image, and this will be the working directory for your application:

WORKDIR /usr/app

Now we will copy files from our folder to our container using the command –

COPY ./ ./

./ is referring to the current directory of our project

Node.js and NPM are pre-installed in this image. Hence the next step is to utilize the npm binary for installing your application dependencies.

RUN npm install

Your app binds to port 8080, so you’ll use the EXPOSE instruction to have it mapped by the docker daemon:

EXPOSE 8080

Finally, specify the runtime command for running your application through CMD. We will utilize “node index.js” to initiate your server in this case.

So, our DockerFile is now completed. It should look like this –

Step 5: Now create a .dockerignore file in the same directory with the following content –

By doing this, you can avoid the risk of your Docker image being overwritten with local modules and debug logs, which will be installed in the image from running Docker images.

Step 6: Building Image

Now, we will build the docker image for this app. Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it’s easier to find later using the docker images command:

docker1

Step 7: Run the image

By executing your image with the -d option, you can run the container in a detached state, which allows it to continue running in the background. Additionally, the -p flag can map a public port to a private one within the container. To start the container from the image that you created earlier, use the following command:

docker2

This command will map the docker port 8080 to your local port 3000. Now you can test on your browser at http://localhost:3000/.

You can run the command docker ps to get the port of your app that docker mapped.

docker3

Step 8: At last, to push this image to your repository, that is to make it public so that anyone can use this image, you need to run

Here you will get a command which anyone can use to pull this docker image.

Conclusion

Dockerization provides a powerful solution for simplifying and streamlining your Node.js application deployment. It empowers you to package your application and its dependencies into portable containers, enabling easier collaboration, improved scalability, and seamless deployment across various environments. By embracing Docker in your Node.js development process, you can unlock a new level of efficiency and reliability, allowing you to focus more on building great applications and less on deployment headaches.

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Docker, NodeJS and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. What is a docker image registry?

ANS: – A Docker image registry, in simple terms, is an area where the Docker images are stored. Instead of converting the applications to containers every time, a developer can directly use the images stored in the registry. This image registry can either be public or private, and Docker Hub is the most popular and famous public registry available.

2. Can a paused container be removed from Docker?

ANS: – No, it is not possible! A container MUST be in the stopped state before we can remove it.

WRITTEN BY Satyam Dhote

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!