A production-ready RESTful API built with Node.js and Express, containerised with Docker for seamless deployment and scalability.
- RESTful API Architecture: Clean and intuitive API endpoints
- Docker Containerization: Consistent development and production environments
- Environment Configuration: Secure configuration management with environment variables
- Error Handling: Comprehensive error handling middleware
- Logging: Structured logging for debugging and monitoring
- CORS Enabled: Cross-Origin Resource Sharing support
- Health Checks: Built-in health check endpoints for monitoring
- Hot Reload: Development environment with automatic server restart
- Runtime: Node.js (v18+)
- Framework: Express.js
- Containerization: Docker & Docker Compose
- Package Manager: npm
- Process Manager: PM2 (optional for production)
- Node.js (v18 or higher)
- Docker Desktop
- Git
# Clone the repository
git clone https://github.com/bdipesh/node_API_docker.git
cd node_API_docker
# Install dependencies
npm install
# Create environment file
cp .env.example .env
# Start development server
npm run devThe API will be available at http://localhost:3000
# Build and start containers
docker-compose up --build
# Run in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Stop containers
docker-compose down# Build production image
docker build -t node-api:prod -f Dockerfile.prod .
# Run production container
docker run -p 3000:3000 --env-file .env node-api:prodThis project uses Docker multi-stage builds to optimise image size and security:
# Stage 1: Dependencies
FROM node:18-alpine AS dependencies
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
# Stage 2: Production
FROM node:18-alpine
WORKDIR /app
COPY --from=dependencies /app/node_modules ./node_modules
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Benefits:
- Smaller Image Size: Only production dependencies are included
- Security: Alpine Linux base reduces attack surface
- Layer Caching: Faster rebuilds by caching dependencies
- Clean Production Build: No development tools in final image
The docker-compose.yml orchestrates multiple services:
version: '3.8'
services:
api:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
volumes:
- ./logs:/app/logs
restart: unless-stoppedKey Features:
- Service Isolation: Each component runs in its own container
- Volume Persistence: Data persists across container restarts
- Network Isolation: Services communicate through Docker networks
- Auto-Restart: Containers automatically restart on failure
- Alpine Linux Base Image: Reduces image size from ~900MB to ~150MB
- Non-Root User: Containers run with limited privileges for security
- Multi-Stage Builds: Separates build and runtime environments
- Layer Optimization: Dependencies copied before application code
- .dockerignore: Excludes unnecessary files from the image
- Health Checks: Docker monitors application health
- Environment Variables: Secrets managed externally
- Minimal Dependencies: Only production packages included
http://localhost:3000/api/v1
GET /healthResponse:
{
"status": "ok",
"timestamp": "2025-10-07T12:00:00.000Z",
"uptime": 3600
}GET /api/v1/itemsResponse:
{
"success": true,
"data": [
{
"id": 1,
"name": "Item 1",
"createdAt": "2025-10-07T12:00:00.000Z"
}
]
}node_API_docker/
βββ src/
β βββ controllers/ # Request handlers
β βββ routes/ # API routes
β βββ middleware/ # Custom middleware
β βββ models/ # Data models
β βββ utils/ # Helper functions
β βββ config/ # Configuration files
βββ tests/ # Test files
βββ logs/ # Application logs
βββ .dockerignore # Docker ignore file
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore file
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Docker build instructions
βββ package.json # Node.js dependencies
βββ server.js # Application entry point
Create a .env file in the root directory:
# Server Configuration
NODE_ENV=development
PORT=3000
HOST=0.0.0.0
# API Configuration
API_VERSION=v1
API_PREFIX=/api
# Database (if applicable)
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=admin
DB_PASSWORD=secret
# Security
JWT_SECRET=your-secret-key
CORS_ORIGIN=*
# Logging
LOG_LEVEL=info- ESLint: Code linting for consistency
- Prettier: Automatic code formatting
- Error Handling: Centralized error handling middleware
- Validation: Input validation using express-validator
- Security Headers: Helmet.js for HTTP security
- Image Size Optimization: Multi-stage builds reduce final image size
- Security Scanning: Regular vulnerability scans with
docker scan - Resource Limits: Memory and CPU constraints defined
- Health Checks: Container health monitoring
- Logging: Proper log management and rotation
- RESTful Conventions: Standard HTTP methods and status codes
- Versioning: API versioning for backward compatibility
- Pagination: Large datasets paginated
- Rate Limiting: Prevents API abuse
- Documentation: Clear API documentation
- Git Flow: Feature branches and pull requests
- Commit Conventions: Conventional commits format
- Code Reviews: Mandatory PR reviews
- CI/CD: Automated testing and deployment
Logs are stored in the logs/ directory and include:
- Access logs (HTTP requests)
- Error logs (application errors)
- Combined logs (all activity)
# View container logs
docker-compose logs api
# Follow logs in real-time
docker-compose logs -f api
# View last 100 lines
docker-compose logs --tail=100 api# Login to Docker Hub
docker login
# Tag image
docker tag node-api:latest yourusername/node-api:latest
# Push to Docker Hub
docker push yourusername/node-api:latestAWS ECS / Azure Container Instances / Google Cloud Run
# Build for production
docker build -t node-api:prod -f Dockerfile.prod .
# Deploy using cloud-specific CLI
# (Follow cloud provider documentation)- Development: Hot reload, verbose logging
- Staging: Production-like, with debugging enabled
- Production: Optimized, minimal logging, security hardened
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Dipesh Basnet
- GitHub: @bdipesh
- LinkedIn: Connect in LinkedIn
- Express.js team for the excellent framework
- Docker community for containerization best practices
- Node.js community for continuous support
β If you find this project helpful, please consider giving it a star!