f4fc960469cd5a977b885929d3e08dc31b9e627d
Homework Backend - Docker & Jenkins CI/CD Setup
This project includes complete CI/CD setup with Jenkins pipeline and Docker containerization.
🏗️ Build & Deploy
Local Development
-
Build and run locally:
# Build the application ./build.sh # Run with Docker Compose docker-compose up --build -
Access the application:
- API: http://localhost:8080
- Swagger UI: http://localhost:8080/swagger
Jenkins Pipeline
The Jenkins pipeline includes the following stages:
- Checkout - Clone the repository
- Restore Dependencies - Install NuGet packages
- Build - Compile the application
- Test - Run unit tests
- Publish - Create deployment artifacts
- Build Docker Image - Create Docker image
- Push Docker Image - Push to registry
- Deploy to Staging - Deploy to staging environment
- Integration Tests - Run tests against staging
- Deploy to Production - Manual approval then production deploy
Environment Variables
Set these environment variables for Jenkins:
DOCKER_REGISTRY=your-registry.com
DB_PASSWORD=your-database-password
Docker Registry Credentials
Configure Docker registry credentials in Jenkins:
- Credential ID:
docker-registry-credentials - Type: Username with password
📁 File Structure
├── Dockerfile # Multi-stage Docker build
├── Jenkinsfile # Jenkins pipeline definition
├── docker-compose.yml # Local development setup
├── docker-compose.staging.yml # Staging environment
├── docker-compose.prod.yml # Production environment
├── .dockerignore # Docker build exclusions
├── build.sh # Local build script
├── deploy.sh # Deployment script
├── init.sql # Database initialization
├── nginx/
│ └── nginx.conf # Nginx reverse proxy config
└── Homework/
├── appsettings.json # Application configuration
└── ... # Application code
🚀 Deployment Environments
Development
- Single container setup with PostgreSQL
- Hot reload enabled
- Logs mounted to host
Staging
- Load balanced (2 replicas)
- Resource limits applied
- Integration testing environment
Production
- Load balanced (3 replicas)
- Nginx reverse proxy with SSL
- Resource limits and health checks
- Database persistence
🔧 Configuration
Application Settings
Update Homework/appsettings.json for different environments:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"JwtSettings": {
"Key": "your-jwt-secret-key",
"Issuer": "your-issuer",
"Audience": "your-audience"
},
"ConnectionStrings": {
"DefaultConnection": "Host=postgres;Database=homework;Username=homework_user;Password=homework_password"
}
}
Jenkins Pipeline Customization
Modify the Jenkinsfile to:
- Change Docker registry URL
- Add additional test stages
- Modify deployment targets
- Add notification channels
🏥 Health Checks
The application includes health check endpoints:
/health- Basic health check- Container health checks configured in Docker
🔒 Security
- Non-root user in containers
- Security headers in Nginx
- Resource limits applied
- Secrets management via environment variables
📊 Monitoring
- Application logs are written to files and console
- Nginx access logs
- Docker container logs
- Health check monitoring
🐛 Troubleshooting
Common Issues
- Build fails: Check .NET SDK version in Dockerfile
- Database connection: Verify PostgreSQL credentials
- Port conflicts: Ensure ports 8080, 5432 are available
- Registry push fails: Check Docker registry credentials
Logs
# View application logs
docker-compose logs homework-backend
# View database logs
docker-compose logs postgres
# View all logs
docker-compose logs
Languages
C#
84.6%
Shell
9.8%
Dockerfile
5.6%