"Dockerizing a Node.js Web App: A Simple Hello Node Program"

Simplifying Deployment with Docker Containers

Introduction

Docker has evolved beyond being solely a DevOps tool and has now become an essential tool for developers as well. In today's software development landscape, having a basic understanding of Docker is highly valuable and can greatly benefit developers. Docker provides developers with the ability to create lightweight and isolated containers that package applications and their dependencies together. This allows for consistent and reproducible environments across different development stages, making it easier to share and collaborate on projects.

In this article, I will guide you through Dockerizing a Node.js web application, showcasing a simple "Hello, Node!" program.

If you're curious, you can also explore the project through my GitHub repository.

https://github.com/Prateema1/Docker_Node_App

Prerequisites:

Before we dive in, make sure you have the following prerequisites installed on your machine:

  1. Docker: Ensure you have the latest version of Docker installed and running.

  2. Node.js and npm: Install the latest stable versions of Node.js and npm.

Let’s start

Step 1: Create a Basic Node.js Web App

Let's start by building a basic Node.js web application without any framework, which will display a "Hello, Node!" message. Open your preferred code editor and create a new project directory. Navigate to the project directory in your terminal and execute the following commands:

# Initialize a new Node.js project
$ npm init -y

Now, create a new file named index.js and add the following code:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3001;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello From Node, I am inside a Docker Container!');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Save the file and close your editor.

Step 2: Create a Dockerfile

To Dockerize our Node.js web application, we need to create a Dockerfile. This file will contain instructions for Docker to build a container image with our application.

In the project directory, create a new file named Dockerfile (without any file extensions) and add the following content:

Dockerfile preview in my code repo

Here's an explanation of each line of code in the given Dockerfile:

FROM node:lts-alpine

This line sets the base image for the Docker container as node:lts-alpine. It means we are using the Node.js runtime with the latest LTS (Long Term Support) version based on the Alpine Linux distribution. This base image provides a lightweight and efficient environment for running Node.js applications.


WORKDIR /app

This line sets the working directory inside the container to /app. It means that any subsequent instructions will be executed in this directory.

COPY package*.json ./

This line copies the package.json and package-lock.json files from the host (the directory where the Dockerfile is located) to the /app directory inside the container. The ./ refers to the current directory on the host.

RUN npm install

This line runs the npm install command inside the container. It installs the dependencies specified in the package.json file. This step is crucial for setting up the application's dependencies within the container.

COPY . /app

This line copies all the files and directories from the host to the /app directory inside the container. It includes the application code and any additional files required for the application to run.

EXPOSE 3001

This line exposes port 3001 on the container. It indicates that the container will listen on this port, allowing external connections to access the application running inside.

CMD ["node", "index.js"]

This line specifies the command to run when the container starts. It runs the Node.js runtime (node) and executes the index.js file. This is typically the entry point of the Node.js application.

These instructions combined create an environment where the Node.js application can run inside a Docker container.

Save the Dockerfile.

Step 3: Build and Run the Docker Image

With the Dockerfile ready, we can now build a Docker image for our Node.js web application.

Open your terminal and navigate to the project directory. Execute the following command to build the Docker image:

docker build -t node-app .

The command above instructs Docker to build an image with the tag node-app. The . indicates that the Dockerfile is present in the current directory.

Once the build process completes successfully, we can run the Docker image using the following command:

docker run -p 3001:3001 node-app

The -p flag maps the host's port 3001 to the container's port 3001, allowing access to the web application running inside the container.

Step 4: Test the Web Application

To verify that our Dockerized Node.js web application is running correctly, open your web browser and navigate to http://localhost:3001/. You should see the "Hello From Node, I am inside a Docker Container!" message displayed.

Useful Docker Commands

1) docker images: The docker images command is used to list the Docker images available on a Docker host. It provides information about the images, such as their repository, tag, image ID, size, and creation date.
2) docker ps: The docker ps command is used to list the currently running containers on a Docker host. It provides information about the containers, including their names, IDs, status, ports, and other details.

Docker Desktop

Graphical User Interface (GUI): Docker Desktop offers a user-friendly GUI that simplifies working with Docker containers and images. The GUI provides a visual representation of containers, images, and networks, allowing users to easily manage and monitor their Docker resources.

Let's visualize it for our application:

Running Docker Container

Running Docker image

Conclusion

In summary, having a basic knowledge of Docker has become increasingly important for developers. It provides them with the ability to create consistent development environments, streamline the deployment process, improve collaboration, and leverage a wide range of pre-built containers and tools. By embracing Docker as a developer tool, developers can enhance their productivity and efficiency in today's fast-paced software development landscape.

Congratulations! You have successfully Dockerized a Node.js + Express web application.

Thanks for Reading! 🙌
See you in the next blog! Until then, keep learning and sharing.

Let’s connect: