DEV Community

Cover image for Containerizing Node.js Applications with Docker
Srinivas Kandukuri
Srinivas Kandukuri

Posted on

Containerizing Node.js Applications with Docker

❓ What is Docker ..?

1.Docker is a tool (PaaS) to make it easier to create, deploy, and run applications by using containers.
2.Docker uses OS level virtualization to deliver a software as a package called container.
3.Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
4.Containers are designed to make it easier to provide a consistent experience as developers and system administrators move code from development environment into production in a fast and replicable way.

Docker is a powerful tool that allows you to package and deploy applications in lightweight containers. In this article, we will walk through the process of containerizing a Node.js application using Docker.

Before we begin, make sure you have Docker installed on your system. You can check if Docker is installed by running the following command:

docker --version
Enter fullscreen mode Exit fullscreen mode

Step 1: Create a Node.js application

First, let's create a simple Node.js application that we can containerize. For this example, we will create a simple "Hello, World!" server using the Express framework.

Create a new directory for your project and navigate to it:

mkdir node-docker-app
cd node-docker-app
Enter fullscreen mode Exit fullscreen mode

Next, create a file called server.js in the project directory and add the following code:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

This code creates a simple Express server that listens on port 3000 and responds to HTTP GET requests with the message "Hello, World!".

Next, create a package.json file in the project directory by running the following command:

npm init -y
Enter fullscreen mode Exit fullscreen mode

This will create a package.json file with default values.

Next, install the Express framework by running the following command:

npm install express
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Dockerfile

A Dockerfile is a text file that contains the instructions for building a Docker image. We will use a Dockerfile to build an image for our Node.js application.

Create a file called Dockerfile in the project directory and add the following code:

FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

This Dockerfile does the following:

It starts with a base image node:14, which is a Docker image that includes Node.js version 14.

It sets the working directory to /app.

It copies the package.json and package-lock.json files to the working directory.

It runs npm install to install the dependencies listed in the package.json file.

It copies all the files in the current directory (i.e. the project directory) to the working directory.

It exposes port 3000, which is the port our application is listening on.

It sets the command to run when the container starts as ["node", "server.js"], which will start the Node.js server.

Step 3: Build the Docker image

Now that we have a Dockerfile, we can use it to build a Docker image for our Node.js application.

To build the image, run the following command in the

project directory:

docker build -t node-docker-app .
Enter fullscreen mode Exit fullscreen mode

This will build the Docker image and give it the name node-docker-app. The . at the end of the command specifies the current directory as the build context, which is the directory that contains the Dockerfile and the files that will be copied into the image.

The build process may take a few minutes to complete. Once it is finished, you can check that the image was successfully created by running the following command:

docker images
Enter fullscreen mode Exit fullscreen mode

This command will list all the Docker images on your system. You should see the node-docker-app image in the list.

Step 4: Run the Docker container

Now that we have a Docker image for our Node.js application, we can run it as a container.

To run the container, use the following command:

docker run -p 3000:3000 node-docker-app
Enter fullscreen mode Exit fullscreen mode

This command runs the node-docker-app image as a container and maps the container's port 3000 to the host's port 3000.

You should see the message "Server is listening on port 3000" in the output. This means that the Node.js server is running inside the container.

You can test the server by opening a web browser and navigating to http://localhost:3000. You should see the message "Hello, World!" displayed in the browser.

Conclusion
In this tutorial, we learned how to containerize a Node.js application using Docker. We created a simple Node.js application, created a Dockerfile to build an image for the application, and ran the image as a container.

This is just a basic example of containerizing a Node.js application with Docker. There are many other options and configurations that you can use to customize the containerization process. For more information, you can refer to the Docker documentation.

Top comments (0)