This guide explains how to run the CRUD Notes API using Docker in both development and production environments.
- Docker v20.10+
- Docker Compose v2.0+
- 8GB+ RAM recommended
- 10GB+ free disk space
# Start development environment
./docker-run.sh start dev
# Stop development environment
./docker-run.sh stop dev
# View logs
./docker-run.sh logs dev# Start production environment
./docker-run.sh start prod
# Stop production environment
./docker-run.sh stop prod
# View logs
./docker-run.sh logs prod- API: http://localhost:3000
- Debug Port: 9229 (for VS Code debugging)
- MongoDB: localhost:27017
- Hot Reload: Enabled with nodemon
- Environment:
.env.development
Services:
notes-api-dev: Node.js API with live reloadmongodb-dev: MongoDB 7.0 for development
- API: http://localhost:3000 (direct) or http://localhost:80 (via Nginx)
- MongoDB: localhost:27017
- Redis: localhost:6379
- Nginx: http://localhost:80, https://localhost:443
- Environment:
.env.production
Services:
notes-api: Optimized Node.js APImongodb: MongoDB 7.0 with persistent storageredis: Redis 7 for caching and sessionsnginx: Nginx reverse proxy with SSL ready
Check application health:
# Development
curl http://localhost:3000/health
# Production (direct)
curl http://localhost:3000/health
# Production (via Nginx)
curl http://localhost:80/healthExpected response:
{
"status": "healthy",
"timestamp": "2025-10-03T16:13:32.633Z",
"uptime": 15.07280759,
"environment": "development|production"
}The docker-run.sh script provides comprehensive Docker management:
# Available commands
./docker-run.sh start [dev|prod] # Start environment
./docker-run.sh stop [dev|prod] # Stop environment
./docker-run.sh restart [dev|prod] # Restart environment
./docker-run.sh logs [dev|prod] # View logs
./docker-run.sh clean # Clean up resources
./docker-run.sh test # Run tests in container
./docker-run.sh status # Show status
./docker-run.sh help # Show helpAlternative Docker commands via npm:
npm run compose:dev # Start development
npm run compose:up # Start production
npm run compose:down # Stop all services
npm run compose:logs # View logs
npm run compose:restart # Restart API serviceNODE_ENV=development
PORT=3000
MONGODB_URI=mongodb://mongodb-dev:27017/crud_notes_dev
JWT_SECRET=dev_secret_key_here
JWT_EXPIRE=24h
BCRYPT_SALT=10NODE_ENV=production
PORT=3000
MONGODB_URI=mongodb://mongodb:27017/crud_notes
JWT_SECRET=production_secret_key_here
JWT_EXPIRE=24h
BCRYPT_SALT=12
REDIS_URL=redis://redis:6379- Non-root user execution (nodejs:1001)
- Read-only root filesystem
- Limited CPU and memory resources
- Security headers via Nginx
- SSL/TLS ready configuration
- Network isolation
- Isolated development network
- Separate database instance
- Debug port access control
crud_notes_api_mongodb_dev_data: Development database
crud_notes_api_mongodb_data: Production database- Automated backups recommended
- Ensure development environment is running
- Attach debugger to port 9229
- Set breakpoints in your IDE
# Enter container shell
docker exec -it notes-api-dev sh # Development
docker exec -it notes-api sh # Production
# View container logs
docker compose -f docker-compose.dev.yml logs -f notes-api-dev
docker compose logs -f notes-api
# Check container health
docker compose ps- Multi-stage Docker builds
- Production-only dependencies
- Alpine Linux base images
- Nginx reverse proxy
- Redis caching layer
- Health checks and auto-restart
- API: 512MB RAM, 0.5 CPU
- MongoDB: 1GB RAM, 1 CPU
- Redis: 256MB RAM, 0.25 CPU
- Nginx: 128MB RAM, 0.25 CPU
Run tests in containerized environment:
# Run all tests
./docker-run.sh test
# Current test coverage: 90.57%
# - Overall: 90.57% (106 tests)
# - Controllers: 88.54%
# - Models: 100%
# - Routes: 88.46%- Port conflicts: Ensure ports 3000, 27017, 6379, 80, 443, 9229 are available
- Memory issues: Increase Docker memory allocation to 8GB+
- Permission errors: Ensure Docker daemon is running with proper permissions
- Network issues: Check firewall settings and Docker network configuration
# API logs
./docker-run.sh logs [dev|prod]
# MongoDB logs
docker compose logs mongodb
docker compose -f docker-compose.dev.yml logs mongodb-dev
# Nginx logs
docker compose logs nginx# Check container status
docker compose ps
# Restart specific service
docker compose restart notes-api
# Force recreate
docker compose up --force-recreate notes-apiThe Nginx configuration supports SSL. To enable HTTPS:
- Place SSL certificates in
./nginx/ssl/ - Update
nginx.confSSL configuration - Restart Nginx container
# Backup development database
docker exec notes-mongodb-dev mongodump --out /backup
# Backup production database
docker exec notes-mongodb mongodump --out /backup# Restore to development
docker exec notes-mongodb-dev mongorestore /backup
# Restore to production
docker exec notes-mongodb mongorestore /backupFor production scaling:
- Use Docker Swarm or Kubernetes
- Implement load balancer
- Set up MongoDB replica set
- Configure Redis cluster
- Monitor with application metrics
For issues and questions:
- Check container logs first
- Verify environment configuration
- Ensure all required ports are available
- Review Docker and system resource usage