codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

42 / 63 topics
40Deploying Node.js Applications41Cloud Services42Containerization with Docker43Monitoring Node.js Applications
Tutorials/Node.js/Containerization with Docker
🟢Node.js

Containerization with Docker

Updated 2026-04-20
3 min read

Containerization with Docker

Introduction

Containerization is a method of deploying and running applications in isolated environments called containers. Docker, an open-source platform, has become the de facto standard for containerization. In this tutorial, we will explore how to containerize a Node.js application using Docker.

Prerequisites

Before you begin, ensure that you have the following installed on your machine:

  • Node.js: Version 14 or higher.
  • Docker Desktop: The latest version of Docker Desktop for Windows, Mac, or Linux.

Step-by-Step Guide to Containerizing a Node.js Application

Step 1: Create a Simple Node.js Application

First, let's create a simple Node.js application. If you already have an existing application, skip this step and proceed to the next one.

mkdir my-node-app
cd my-node-app
npm init -y

Create a file named index.js with the following content:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
});

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

Step 2: Create a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here’s how you can create one for your Node.js application.

Create a file named Dockerfile in the root of your project directory with the following content:

# Use an official Node runtime as a parent image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Bundle app source inside Docker image
COPY . .

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Define environment variable
ENV NODE_ENV production

# Run app.js when the container launches
CMD ["node", "index.js"]

Step 3: Build the Docker Image

Now, let's build the Docker image using the Dockerfile. Open your terminal and run the following command from the root of your project directory:

docker build -t my-node-app .

This command builds a Docker image with the tag my-node-app.

Step 4: Run the Docker Container

Once the image is built, you can run it as a container. Use the following command to start the container:

docker run -p 3000:3000 my-node-app

This command maps port 3000 of your host machine to port 3000 in the Docker container, allowing you to access the application at http://localhost:3000.

Step 5: Verify the Application

Open a web browser and navigate to http://localhost:3000. You should see "Hello World" displayed on the page.

Best Practices for Containerizing Node.js Applications

  • Use Official Base Images: Always use official base images from Docker Hub, such as node:14, which are regularly updated and secure.

  • Minimize Image Size: Use multi-stage builds to reduce the size of your final image. For example:

    # Stage 1: Build the application
    FROM node:14 AS build
    WORKDIR /usr/src/app
    COPY package*.json ./
    RUN npm install
    COPY . .
    RUN npm run build
    
    # Stage 2: Serve the application
    FROM node:14-alpine
    WORKDIR /usr/src/app
    COPY --from=build /usr/src/app/dist ./dist
    EXPOSE 3000
    CMD ["node", "index.js"]
    
  • Environment Variables: Use environment variables for configuration settings to make your application more flexible and secure.

  • Security: Regularly update your base images and dependencies. Use tools like docker scan to check for vulnerabilities in your Docker image.

  • Logging: Redirect logs to standard output and error streams so they can be captured by Docker or Kubernetes.

Conclusion

Containerizing a Node.js application with Docker is a powerful way to ensure consistency across different environments and simplify deployment. By following the steps outlined in this tutorial, you should now have a basic understanding of how to containerize your Node.js applications using Docker. As you gain more experience, explore advanced features like Docker Compose for multi-container applications or Kubernetes for orchestration.


PreviousCloud ServicesNext Monitoring Node.js Applications

Recommended Gear

Cloud ServicesMonitoring Node.js Applications