Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Note: package-lock.json is needed for npm ci in Docker

# Environment variables
.env
.env.local
.env.*.local

# Logs
logs/
*.log

# Testing
coverage/
.nyc_output/

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store

# Git
.git/
.gitignore
.gitattributes

# CI/CD
.github/
.gitlab-ci.yml

# Docker
Dockerfile
docker-compose.yml
.dockerignore

# Documentation
README.md
CHANGELOG.md
LICENSE
CODE_OF_CONDUCT.md

# Misc
.husky/
.commitlintrc*
.eslintrc*
.prettierrc*
eslint.config.js
commitlint.config.js
*.md
23 changes: 18 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Example environment variables for RBAC app
# Application Configuration
NODE_ENV=production
PORT=5000
MONGO_URI=mongodb://root:admin@localhost:27017/
JWT_SECRET=your_jwt_secret_here
RESEND_API_KEY=""
CORS_URL=http://localhost:5173
APP_PORT=5000

# MongoDB Configuration
MONGO_ROOT_USERNAME=admin
MONGO_ROOT_PASSWORD=admin123
MONGO_DB_NAME=rbac_db
MONGO_PORT=27017
MONGO_URI=mongodb://admin:admin123@mongodb:27017/rbac_db?authSource=admin

# JWT Configuration
JWT_SECRET=your_jwt_secret_key_change_in_production
JWT_EXPIRE=7d

# CORS Configuration
CORS_URL=http://localhost:3000

25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
node_modules

# Environment variables
.env
.env.*.local
.env.local
.env.production

# Build directories
/dist/
/build/

# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Docker volumes (if running locally)
mongodb_data/
mongodb_config/
268 changes: 268 additions & 0 deletions DOCKERIZATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
# Dockerization Summary

## ✅ Completed Tasks

This document summarizes the Docker implementation for the RBAC application.

### 1. ✅ Dockerfile Creation

**File:** `Dockerfile`

**Features implemented:**

- Multi-stage build using `node:18-alpine` for smaller image size
- Optimized for build caching (dependencies installed before copying source code)
- Separate stages for development and production
- Non-root user (`nodejs`) for enhanced security
- Uses `dumb-init` for proper signal handling
- Production image size optimized with production-only dependencies
- Development stage includes hot reload with nodemon

**Security best practices:**

- Runs as non-root user (UID 1001, GID 1001)
- Minimal attack surface with Alpine Linux
- Separate build stages
- No unnecessary files in final image

### 2. ✅ .dockerignore File

**File:** `.dockerignore`

**Excludes:**

- `node_modules/` (dependencies installed in container)
- `.env` files (for security)
- Log files
- IDE/editor configurations
- Git files
- Documentation files
- CI/CD configurations
- Docker files themselves

**Benefits:**

- Smaller build context
- Faster build times
- Enhanced security (no sensitive files)
- Reduced image size

### 3. ✅ Docker Compose Configuration

**File:** `docker-compose.yml`

**Services implemented:**

1. **MongoDB Service (`mongodb`)**
- Official MongoDB 7 image
- Persistent data with named volumes
- Health checks implemented
- Configurable credentials via environment variables
- Exposed on port 27017 (configurable)

2. **Application Service (`app`)**
- Production-ready Node.js service
- Depends on MongoDB health check
- Health check endpoint configured
- Configurable via environment variables
- Exposed on port 5000 (configurable)
- Restart policy: `unless-stopped`

3. **Development Service (`app-dev`)**
- Activated with `--profile dev` flag
- Volume mounts for hot reload
- Nodemon for automatic restart
- Same configuration as production but with dev dependencies

**Volumes:**

- `mongodb_data` - MongoDB data persistence
- `mongodb_config` - MongoDB configuration persistence

**Network:**

- Custom bridge network (`rbac-network`) for service isolation

**Features:**

- Service health checks
- Automatic restart policies
- Environment variable configuration
- Profile-based service activation (dev/prod)
- Service dependencies management

### 4. ✅ Environment Configuration

**File:** `.env.example`

**Variables configured:**

- `NODE_ENV` - Application environment
- `PORT` - Application port
- `MONGO_URI` - MongoDB connection string
- `MONGO_ROOT_USERNAME` - Database username
- `MONGO_ROOT_PASSWORD` - Database password
- `MONGO_DB_NAME` - Database name
- `JWT_SECRET` - JWT signing key
- `JWT_EXPIRE` - Token expiration time
- `CORS_URL` - CORS allowed origin

**Security notes:**

- Contains example/default values
- Actual `.env` file is gitignored
- Production values should be changed

### 5. ✅ Documentation

**Files created:**

1. **README.Docker.md** - Comprehensive Docker guide
- Prerequisites
- Quick start instructions
- Detailed usage examples
- Environment variable reference
- Security best practices
- Troubleshooting guide
- Production deployment guidelines
- Useful commands reference

2. **DOCKER_QUICK_REFERENCE.md** - Quick command reference
- Common Docker commands
- Docker Compose commands
- Monitoring commands
- Debugging commands
- Database operations
- Cleanup commands

3. **Updated README.md** - Added Docker section
- Quick start with Docker
- Link to detailed documentation
- Feature highlights

### 6. ✅ Application Enhancements

**Changes made:**

1. **Health Check Endpoint**
- Added `/api/auth/health` endpoint in `authRoutes.js`
- Returns service status and timestamp
- Used by Docker health checks

2. **Package.json Scripts**
- Added `start` script for production
- Added Docker convenience scripts:
- `docker:build` - Build Docker image
- `docker:up` - Start services
- `docker:down` - Stop services
- `docker:logs` - View logs
- `docker:dev` - Start in development mode

3. **Updated .gitignore**
- Added Docker-related ignores
- Added log file patterns
- Added IDE/OS-specific patterns

## 📊 Acceptance Criteria Status

| Criterion | Status | Implementation |
| -------------------------------------------------------------- | ----------- | -------------------------------------------------------- |
| Create Dockerfile with official Node.js image (node:18-alpine) | ✅ Complete | Multi-stage Dockerfile with Alpine Linux |
| Optimize for build caching | ✅ Complete | Dependencies copied and installed before source code |
| Create .dockerignore file | ✅ Complete | Excludes node_modules, .env, logs, and unnecessary files |
| Run as non-root user | ✅ Complete | Uses `nodejs` user (UID 1001, GID 1001) |
| (Bonus) Docker Compose with MongoDB | ✅ Complete | Full docker-compose.yml with MongoDB service |

## 🚀 Usage

### Quick Start (Production)

```bash
cp .env.example .env
docker compose up -d
```

### Development Mode

```bash
docker compose --profile dev up -d app-dev
```

### Stop Services

```bash
docker compose down
```

## 🔐 Security Features

1. **Non-root user execution** - App runs as `nodejs` user
2. **Minimal base image** - Alpine Linux reduces attack surface
3. **Environment variable isolation** - Secrets not baked into image
4. **.dockerignore** - Prevents sensitive file inclusion
5. **Health checks** - Monitors service health
6. **Network isolation** - Custom Docker network

## 📈 Performance Optimizations

1. **Multi-stage builds** - Smaller final image
2. **Build caching** - Faster subsequent builds
3. **Alpine Linux** - Reduced image size (~70MB vs ~900MB)
4. **Production dependencies only** - Smaller runtime image
5. **Layer optimization** - Efficient Docker layer caching

## 🧪 Testing

To verify the Docker setup:

```bash
# Build and start
docker compose up -d

# Check services are running
docker compose ps

# Test health endpoint
curl http://localhost:5000/api/auth/health

# View logs
docker compose logs -f

# Clean up
docker compose down
```

## 📝 Additional Notes

- Docker Compose v2 syntax used (`docker compose` instead of `docker-compose`)
- Compatible with both v1 and v2
- MongoDB data persists in Docker volumes
- Development mode supports hot reload
- Production-ready with security best practices
- Comprehensive documentation provided

## 🎯 Benefits

1. **Consistency** - Same environment across development, testing, and production
2. **Portability** - Run anywhere Docker runs
3. **Isolation** - No dependency conflicts with host system
4. **Scalability** - Easy to scale with orchestration tools
5. **Easy onboarding** - New developers can start quickly
6. **Production-ready** - Follows Docker best practices

## 🔄 Future Enhancements (Optional)

- [ ] Add Nginx reverse proxy
- [ ] Implement Docker Secrets for production
- [ ] Add monitoring with Prometheus/Grafana
- [ ] Multi-architecture builds (ARM64 support)
- [ ] CI/CD pipeline integration
- [ ] Kubernetes manifests
- [ ] Redis caching layer

---

**Implementation Date:** October 30, 2025
**Docker Version:** 28.5.0
**Docker Compose Version:** v2.33.1
**Status:** ✅ Complete and tested
Loading
Loading