โ— Shell
clean mode source โ†—

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