Apps Development, Cloud Computing, DevOps

4 Mins Read

Dockerizing NodeJS Applications for Seamless Deployment

Voiced by Amazon Polly

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.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

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.

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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

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!