Repository navigation
Expand file tree
/
Copy path04-environment-application.mdc
More file actions
247 lines (206 loc) · 8.59 KB
/
Copy path04-environment-application.mdc
File metadata and controls
247 lines (206 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---
description:
globs:
alwaysApply: false
---
# Environment & Application Configuration Standards
**Description**: Environment setup, application configuration, and deployment management standards for containerized applications. Apply when setting up environments, managing configurations, implementing secret management, or organizing project documentation.
**Key Focus Areas**: Development environments, configuration management, secret handling, environment separation, documentation structure
## Development Environment Standards
### Containerized Development Setup
- **Container-First Development**: All development should happen in containerized environments
- **Environment Parity**: Development, staging, and production environments should be as similar as possible
- **Reproducible Builds**: Ensure consistent builds across different developer machines
- **Dependency Isolation**: Use containers to isolate application dependencies
- **Version Consistency**: Lock dependency versions to ensure consistency
### Development Tools
- **Hot Reloading**: Enable live code reloading in development containers
- **Debug Ports**: Expose debugging ports only in development environment
- **Development Dependencies**: Separate development dependencies from production
- **Testing Framework**: Include testing frameworks and tools in development setup
## Configuration Management
### Configuration Hierarchy
1. **Default Values**: Hardcoded defaults in application code
2. **Configuration Files**: Environment-specific config files
3. **Environment Variables**: Runtime configuration via environment variables
4. **Secrets**: Sensitive data through secret management systems
5. **Command Line Arguments**: Override configuration at runtime
### Environment Variable Standards
- **Naming Convention**: Use consistent, descriptive environment variable names
- **Service URLs**: Use internal service names for inter-service communication
- **Feature Flags**: Support feature toggles through environment variables
- **Logging Configuration**: Configure logging levels and formats via environment
- **Monitoring Endpoints**: Define health check and metrics endpoints
### Configuration File Patterns
```yaml
# config/default.yml
app:
name: myapp
port: 3000
host: 0.0.0.0
database:
host: localhost
port: 5432
name: myapp
logging:
level: info
format: json
---
# config/production.yml
app:
host: 0.0.0.0
database:
host: ${DB_HOST}
port: ${DB_PORT}
name: ${DB_NAME}
logging:
level: warn
```
### External Configuration
- **Configuration Management**: Use external configuration management systems for complex deployments
- **Environment Separation**: Maintain separate configurations for each environment
- **Configuration Validation**: Validate configuration at application startup
- **Hot Reloading**: Support configuration hot reloading where appropriate
## Secret Management
### Secret Handling Principles
- **Never Commit Secrets**: Never store secrets in version control
- **Least Privilege**: Grant minimal necessary access to secrets
- **Rotation**: Implement secret rotation policies
- **Encryption**: Encrypt secrets at rest and in transit
- **Audit Trail**: Maintain audit logs for secret access
### Docker Secrets Integration
- **Docker Secrets**: Use Docker secrets for production deployments
- **File-Based Secrets**: Reference secrets via file paths in containers
- **External Secret Management**: Integrate with enterprise secret management systems
### Environment-Specific Secrets
- **Development**: Use dummy secrets or local secret files
- **Staging**: Use staging-specific secrets with limited scope
- **Production**: Use enterprise secret management (HashiCorp Vault, AWS Secrets Manager, etc.)
### Secret File Patterns
```bash
# .env.example (template file)
DB_PASSWORD=your_secure_password_here
API_KEY=your_api_key_here
JWT_SECRET=your_jwt_secret_here
# .env.local (gitignored actual values)
DB_PASSWORD=actual_secure_password
API_KEY=actual_api_key
JWT_SECRET=actual_jwt_secret
```
## Environment Separation
### Environment Types
- **Development**: Local development with debug features enabled
- **Testing**: Automated testing environment with test data
- **Staging**: Production-like environment for final testing
- **Production**: Live environment serving real users
### Environment-Specific Configurations
```yaml
# Base docker-compose.yml
version: '3.8'
services:
app:
image: myapp:${APP_VERSION:-latest}
environment:
- APP_ENV=${APP_ENV:-production}
---
# docker-compose.dev.yml
version: '3.8'
services:
app:
build: .
volumes:
- .:/app:cached
environment:
- APP_ENV=development
- DEBUG=true
---
# docker-compose.prod.yml
version: '3.8'
services:
app:
image: myapp:${APP_VERSION}
deploy:
replicas: 3
resources:
limits:
memory: 512M
environment:
- APP_ENV=production
```
### Environment Validation
- **Configuration Checks**: Validate environment configuration at startup
- **Health Checks**: Implement comprehensive health checks for each environment
- **Smoke Tests**: Run smoke tests after deployment to each environment
- **Monitoring**: Implement environment-specific monitoring and alerting
## Documentation Structure
### Required Documentation Categories
#### `/docs/architecture/` - System Design
- **system-overview.md**: High-level system architecture
- **service-architecture.md**: Service communication and dependencies
- **docker-architecture.md**: Container and orchestration setup
- **database-design.md**: Data models and relationships
- **security-architecture.md**: Security patterns and implementations
#### `/docs/development/` - Developer Resources
- **getting-started.md**: Setup instructions for new developers
- **development-workflow.md**: Local development processes
- **coding-standards.md**: Code quality and style guidelines
- **testing-guide.md**: Testing strategies and frameworks
- **debugging-guide.md**: Common issues and solutions
#### `/docs/deployment/` - Operations
- **deployment-guide.md**: Step-by-step deployment procedures
- **environment-setup.md**: Environment configuration instructions
- **docker-setup.md**: Container orchestration setup
- **monitoring-setup.md**: Observability and monitoring configuration
- **disaster-recovery.md**: Backup and recovery procedures
#### `/docs/user-guides/` - End User Documentation
- **user-manual.md**: Complete user guide
- **api-documentation.md**: API reference and examples
- **troubleshooting.md**: Common user issues and solutions
- **feature-guides/**: Detailed feature documentation
#### `/docs/reference/` - Technical Reference
- **api-reference.md**: Complete API documentation
- **configuration-reference.md**: All configuration options
- **changelog.md**: Version history and changes
- **glossary.md**: Technical terms and definitions
### Documentation Standards
- **Keep Current**: Update documentation with every code change
- **Practical Examples**: Include working code examples and configurations
- **Environment-Specific**: Document differences between environments
- **Visual Aids**: Use diagrams and screenshots where helpful
- **Searchable**: Structure documentation for easy searching
### Documentation Automation
```yaml
# Example GitHub Actions for documentation
name: Documentation
on:
push:
paths: ['docs/**', '*.md']
jobs:
deploy-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
```
## Application Lifecycle Management
### Startup Procedures
- **Configuration Loading**: Load and validate configuration at startup
- **Health Checks**: Implement readiness and liveness probes
- **Dependency Checking**: Verify external service availability
- **Database Migrations**: Run database migrations automatically
- **Cache Warming**: Pre-populate caches if necessary
### Runtime Configuration
- **Feature Flags**: Implement feature flag systems for runtime control
- **Configuration Refresh**: Support configuration updates without restart
- **Graceful Degradation**: Handle external service failures gracefully
- **Circuit Breakers**: Implement circuit breaker patterns
### Shutdown Procedures
- **Graceful Shutdown**: Handle shutdown signals properly
- **Connection Cleanup**: Close database and external connections
- **Request Completion**: Allow in-flight requests to complete
- **Resource Cleanup**: Clean up temporary files and resources
This comprehensive approach ensures robust, scalable, and maintainable configuration management for containerized applications across all environments.