Simple Node.js Application + Deploy
to Docker Hub
We will create a very simple Node.js web
application, create a Docker image, and then upload (push) it to
Docker Hub.
Step 1: Create a Project Folder
Open VS Code and create a folder:
node-docker-app
Inside it, create these files:
node-docker-app/
│
├──
server.js
├──
package.json
└── Dockerfile
Step 2: Create server.js
Add this simple Node.js code:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end("<h1>Hello from Node.js Docker
App!</h1>");
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});
This program creates a small web server running on port
3000.
Step 3: Create package.json
{
"name": "node-docker-app",
"version": "1.0.0",
"description": "Simple Node.js Docker application",
"main": "server.js",
"scripts": {
"start": "node server.js"
}
}
Since this simple application uses only Node.js
built-in modules, we do not need any additional npm packages.
Step 4: Create a Dockerfile
Create a file named exactly:
Dockerfile
Add:
FROM node:alpine
WORKDIR /app
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Simple Explanation
|
Command |
Meaning |
|
FROM node:alpine |
Uses a lightweight Node.js image |
|
WORKDIR /app |
Creates/uses /app folder |
|
COPY . . |
Copies project files into the container |
|
EXPOSE 3000 |
Declares port 3000 |
|
CMD |
Starts the Node.js application |
Step 5: Build the Docker Image
Open the terminal inside your project folder and run:
docker build -t node-docker-app .
Check the image:
docker images
Step 6: Run the Docker Container
docker run -d -p 3000:3000 --name node-app
node-docker-app
Check the running container:
docker ps
Now open your browser:
http://localhost:3000
You should see:
Hello from Node.js Docker App!
Go to:
Create an account if you do not already have one.
For example, suppose your Docker Hub username is:
your-dockerhub-username
Step 8: Login to Docker Hub
In the terminal, run:
docker login
Enter your Docker Hub username and password when
requested.
Step 9: Tag Your Docker Image
Docker Hub needs your username in the image name.
Replace your-dockerhub-username with your actual
Docker Hub username:
docker tag node-docker-app
your-dockerhub-username/node-docker-app:latest
Example
If your username is sofficial333:
docker tag node-docker-app
sofficial333/node-docker-app:latest
Check it:
docker images
Step 10: Push the Image to Docker Hub 🚀
Run:
docker push your-dockerhub-username/node-docker-app:latest
Example
docker push soffical333/node-docker-app:latest
Docker will upload your image to your Docker Hub
repository.
Complete Flow
Create Node.js App
↓
Create Dockerfile
↓
docker build
↓
Docker Image
↓
docker run
↓
Test on localhost:3000
↓
docker login
↓
docker tag
↓
docker push
↓
☁️
Docker Hub
Pull the Image on Another Computer
Anyone with access to the repository can download it
using:
docker pull
your-dockerhub-username/node-docker-app:latest
Then run it:
docker run -d -p 3000:3000
your-dockerhub-username/node-docker-app:latest
This is the basic workflow:
Create App → Build Image → Run Container →
Tag Image → Push to Docker Hub → Pull and Run Anywhere.
