Skip to content

Bishal07-glitch/Dockerize_NodeJs_WebApp

Repository files navigation

Dockerizing Node.js & Express the Easy Way 🚀

If you’ve ever built a Node.js application and wanted a better way to run it on any computer without worrying about setup issues, Docker is the tool you need. Docker helps developers create containers — lightweight, standalone environments that include everything your app needs to run.

In this guide, you’ll learn how to build and run a simple Node.js Express app inside a Docker container. This is perfect for beginners who are just getting started with backend development and want to explore how Docker can simplify development and deployment.


🛠️ Prerequisites

  • Node.js and npm installed
  • Docker installed
  • Basic understanding of Node.js and Express
  • Text editor like VS Code

📁 Step 1: Initialize a Node.js App

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

📦 Step 2: Create a Simple Express App

Install Express:

npm install express

Then create index.js:

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

app.get('/', (req, res) => {
  res.send('Hello from Dockerized Node.js App!');
});

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

🐳 Step 3: Create a Dockerfile

A Dockerfile contains instructions to build a Docker image for your app.

Create a file named Dockerfile:

# Use Node.js official image
FROM node:18

# Create app directory
WORKDIR /app

# Copy package files and install
COPY package*.json ./
RUN npm install

# Copy the app code
COPY . .

# App will run on port 3000
EXPOSE 3000

# Start the app
CMD ["node", "index.js"]

📄 Step 4: Create .dockerignore

.dockerignore is like .gitignore, it tells Docker what to ignore during the image build.

Create a file named .dockerignore:

node_modules
npm-debug.log

🏗️ Step 5: Build Docker Image

docker build -t node-docker-app .

▶️ Step 6: Run Docker Container

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

Now visit: http://localhost:3000


✅ Conclusion

In this guide, we walked through building a simple Node.js Express application and running it inside a Docker container. Whether you're a beginner or someone trying Docker for the first time, this setup is a great way to understand how modern applications are packaged and deployed.


Author: Bishal Kunwar
🗓️ Published: April 13, 2025
📰 Subscribe to my newsletter for more content like this!


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published