In modern web development, Docker has become an indispensable tool for building, shipping, and running applications. By Dockerizing your Node.js application, you can make it portable, consistent across different environments, and easy to manage.
On this Papayacoders website, we will walk you through every step of containerizing a simple Node.js app.
Dockerize a Node.js Application
Before we begin, make sure you have:-
- Node.js installed (for local testing)
- Docker is installed and running
- A basic understanding of Node.js and Express

Step 1: Create a Simple Node.js Application
Start by creating a new project folder:
mkdir docker-node-app
cd docker-node-app
Initialize a new Node project:
npm init -y
Install Express:
npm install express
Now, create the main app file.
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Dockerized Node.js!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Step 2: Create a package.json
(Auto-generated with npm init
)
Make sure your package.json
Includes the start script:
"scripts": {
"start": "node index.js"
}
Step 3: Create a .dockerignore
File
Exclude files/folders you don’t want in the Docker image:
.dockerignore
node_modules
npm-debug.log
Step 4: Create the Dockerfile
This file defines how your Docker image should be built.
Dockerfile
# Use the official Node.js image
FROM node:18-alpine
# Create app directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
Step 5: Build the Docker Image
Run this command in your project directory:
docker build -t node-docker-app .
This command:
- Reads the Dockerfile
- Installs dependencies
- Creates an image named
node-docker-app
Step 6: Run the Docker Container
Now run your container:
docker run -p 3000:3000 node-docker-app
Open your browser and visit:
http://localhost:3000
You should see: “Hello from Dockerized Node.js!”
Step 7: Stop and Clean Up
To stop the container, press Ctrl + C
.
To list running containers:
docker ps
To stop and remove a container:
docker stop <container_id>
docker rm <container_id>
To remove the image:
docker rmi node-docker-app
Use Docker Compose (Optional)
If your app uses databases (like MongoDB), Docker Compose helps manage multiple containers.
Create a docker-compose.yml
:
version: '3'
services:
app:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
environment:
- NODE_ENV=development
Run it using:
docker-compose up
Benefits of Dockerizing Node.js Apps

Stability: | Same environment in development, testing, and production |
Portability: | Run wherever Docker is supported |
Simplified deployment: | Easily deploy across cloud providers |
Faster onboarding: | New developers can run apps immediately |
Conclusion:-
In simple terms, conclusion, you should know that Dockerizing a Node.js application helps streamline the development and deployment processes. By following the steps above, you can easily package your Node.js app into a container and run it reliably anywhere.
Read also:-