GitHub - atulkamble/docker-nodejs-webapp: 03-05-2025 Docker NodeJS WebApp
๐ฆ Project: Dockerized Node.js Web App
- A simple Node.js web application
- A Dockerfile to containerize it
- A docker-compose.yml to manage services
- Instructions to build and run the container
๐ Project Structure
docker-node-webapp/
โโโ app/
โ โโโ server.js
โ โโโ package.json
โโโ Dockerfile
โโโ .dockerignore
โโโ docker-compose.yml
โโโ README.md
run on Windows
// Open Docker Desktop
# Download and install Chocolatey:
powershell -c "irm https://community.chocolatey.org/install.ps1|iex"
# Download and install Node.js:
choco install nodejs-lts --version="22"
# Verify the Node.js version:
node -v # Should print "v22.17.0".
# Verify npm version:
npm -v # Should print "10.9.2".
git clone https://github.com/atulkamble/docker-nodejs-webapp.git
cd docker-nodejs-webapp
node index.js
docker-compose build
docker-compose up
``
---
---
## ๐ Code Details
---
### ๐ `app/package.json`
```json
{
"name": "docker-node-webapp",
"version": "1.0.0",
"description": "A simple Node.js app running in Docker",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2"
}
}
๐ app/server.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(`App listening at http://localhost:${PORT}`); });
๐ .dockerignore
node_modules
npm-debug.log
๐ Dockerfile
# Use official Node.js base image FROM node:20-alpine # Set working directory WORKDIR /usr/src/app # Copy package files and install dependencies COPY app/package*.json ./ RUN npm install # Copy application source COPY app/ . # Expose port EXPOSE 3000 # Command to run the app CMD [ "npm", "start" ]
๐ docker-compose.yml
version: '3.8' services: web: build: . ports: - "3000:3000" volumes: - ./app:/usr/src/app environment: - NODE_ENV=development
๐ README.md (Optional)
# Dockerized Node.js Web Application ## ๐ณ How to Run 1๏ธโฃ Build the Docker image
docker-compose build
docker-compose up
3๏ธโฃ Visit the app:
[http://localhost:3000](http://localhost:3000)
## ๐ฆ Project Structure
(see directory structure above)
---
๐ How to Use
1๏ธโฃ Clone the project or copy the files locally 2๏ธโฃ Run:
docker-compose build docker-compose up
3๏ธโฃ Open your browser:
โ What You Learn from This
- Writing a Dockerfile for Node.js app
- Ignoring unnecessary files via
.dockerignore - Managing containers using Docker Compose
- Exposing container ports
- Live development via volume mounts