This document provides comprehensive instructions for running the CRUD Notes API using Docker and Docker Compose.
- Docker Engine 20.10+
- Docker Compose 2.0+
- 4GB+ available RAM
- 10GB+ available disk space
- Clone and prepare the environment:
git clone <repository-url>
cd CRUD_Notes_API
cp .env.example .env- Update environment variables in
.env:
# Edit .env file with production values
JWT_SECRET=your-super-secure-jwt-secret-for-production
COOKIE_SECRET=your-super-secure-cookie-secret-for-production- Build and run the application:
docker-compose up -d- Verify the deployment:
# Check container status
docker-compose ps
# Check application health
curl http://localhost:3000/health
# View logs
docker-compose logs -f notes-api- Run development setup:
docker-compose -f docker-compose.dev.yml up -d- Access development features:
- API: http://localhost:3000
- Debug port: 9229 (for Node.js debugging)
- Hot reload enabled
- Development logs
-
notes-api - Main Node.js application
- Port: 3000
- Health check:
/health - Auto-restart enabled
-
mongodb - MongoDB database
- Port: 27017
- Persistent data storage
- Authentication enabled
- Automatic initialization
-
redis (Optional) - Session storage
- Port: 6379
- Memory-based caching
-
nginx (Optional) - Reverse proxy
- Port: 80/443
- Load balancing
- SSL termination
- Security headers
- All services communicate through
notes-network - Internal DNS resolution enabled
- Isolated from external networks
| Variable | Description | Default |
|---|---|---|
NODE_ENV |
Application environment | production |
PORT |
Application port | 3000 |
MONGODB_URI |
MongoDB connection string | Required |
JWT_SECRET |
JWT signing secret | Required |
COOKIE_SECRET |
Cookie signing secret | Required |
- Database:
notes_db - Admin User:
admin/password123 - App User:
notes_user/notes_password - Indexes: Automatically created for performance
curl http://localhost:3000/healthResponse:
{
"status": "healthy",
"timestamp": "2025-10-03T10:30:00.000Z",
"uptime": 3600,
"environment": "production"
}All services include health checks:
- API: HTTP endpoint verification
- MongoDB: Database ping test
- Redis: Connection test
- Nginx: Service availability
# View all container status
docker-compose ps
# Monitor logs
docker-compose logs -f
# Monitor specific service
docker-compose logs -f notes-api
# Check resource usage
docker stats
# Inspect container details
docker inspect notes-api- Non-root user execution
- Secure cookie configuration
- JWT token validation
- Input sanitization
- CORS protection
- Internal network isolation
- Security headers via Nginx
- SSL/TLS support ready
- Rate limiting capable
- MongoDB authentication
- Encrypted connections
- Environment variable isolation
- Persistent volume security
- Start development environment:
docker-compose -f docker-compose.dev.yml up- Access development tools:
- Code changes auto-reload
- Debug port available (9229)
- Development logs enabled
- Hot module replacement
- Run tests in container:
docker-compose exec notes-api-dev npm test
docker-compose exec notes-api-dev npm run test:coverage- Attach debugger to port 9229
- Use VS Code Docker extension
- Execute commands in container:
docker-compose exec notes-api bash- Prepare production environment:
# Create production .env
cp .env.example .env.production
# Update with production values
vim .env.production- Deploy with production compose:
docker-compose -f docker-compose.yml up -d- Setup SSL (Optional):
# Add SSL certificates to ./ssl/ directory
# Update nginx configuration for HTTPSScale specific services:
# Scale API instances
docker-compose up -d --scale notes-api=3
# Scale with load balancer
docker-compose up -d --scale notes-api=3 nginx# Update application
docker-compose build notes-api
docker-compose up -d notes-api
# Rollback if needed
docker-compose down
docker-compose up -d# Create backup
docker-compose exec mongodb mongodump --host localhost --port 27017 --authenticationDatabase admin -u admin -p password123 --out /tmp/backup
# Copy backup from container
docker cp notes-mongodb:/tmp/backup ./backup# Copy backup to container
docker cp ./backup notes-mongodb:/tmp/restore
# Restore database
docker-compose exec mongodb mongorestore --host localhost --port 27017 --authenticationDatabase admin -u admin -p password123 /tmp/restore# Remove stopped containers
docker-compose down
# Remove volumes (β οΈ Data loss!)
docker-compose down -v
# Clean up images
docker system prune -f# Add to docker-compose.yml
services:
notes-api:
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
reservations:
memory: 256M- Redis for session storage
- Nginx for static content
- Database query optimization
# Install Apache Bench
apt-get install apache2-utils
# Test API performance
ab -n 1000 -c 10 http://localhost:3000/api/v1/notes- Port already in use:
# Check what's using the port
lsof -i :3000
# Kill process or change port in .env- Database connection failed:
# Check MongoDB container
docker-compose logs mongodb
# Verify connection string
docker-compose exec notes-api env | grep MONGODB_URI- Container won't start:
# Check container logs
docker-compose logs notes-api
# Verify Dockerfile syntax
docker build -t test .# Enter container shell
docker-compose exec notes-api sh
# Check environment variables
docker-compose exec notes-api printenv
# Test database connection
docker-compose exec notes-api node -e "console.log('Testing DB connection...')"
# Check file permissions
docker-compose exec notes-api ls -laYour CRUD Notes API is now fully dockerized with:
β Production-ready containers β Development environment β Health checks and monitoring β Security best practices β Scalability support β Comprehensive documentation
The application is ready for deployment in any Docker-compatible environment!