-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker-compose.prod.yml
More file actions
238 lines (223 loc) · 7.57 KB
/
docker-compose.prod.yml
File metadata and controls
238 lines (223 loc) · 7.57 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
# =============================================================================
# AccountSafe - Production Docker Compose
# =============================================================================
# AWS EC2 Deployment Configuration
#
# Usage:
# 1. Copy .env.production.example to .env and configure
# 2. Run: make init-ssl (first time only)
# 3. Run: docker-compose -f docker-compose.prod.yml up -d
# =============================================================================
services:
# ===========================================================================
# PostgreSQL Database
# ===========================================================================
db:
image: postgres:15-alpine
container_name: accountsafe-db
restart: always
environment:
POSTGRES_DB: ${DB_NAME:-accountsafe}
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:?Database password required}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-accountsafe}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
networks:
- internal
# Security: No exposed ports - internal network only
# Resource limits for production stability
deploy:
resources:
limits:
cpus: '1'
memory: 1G
reservations:
memory: 256M
# ===========================================================================
# Django Backend (Gunicorn)
# ===========================================================================
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: accountsafe-backend
restart: always
depends_on:
db:
condition: service_healthy
environment:
# Django Core
DEBUG: "False"
SECRET_KEY: ${SECRET_KEY:?Secret key required}
ALLOWED_HOSTS: ${DOMAIN},backend,localhost
# Database (internal Docker network)
DB_NAME: ${DB_NAME:-accountsafe}
DB_USER: ${DB_USER:-postgres}
DB_PASSWORD: ${DB_PASSWORD:?Database password required}
DB_HOST: db
DB_PORT: "5432"
# CORS (production domain with HTTPS)
CORS_ALLOWED_ORIGINS: https://${DOMAIN}
# Email
EMAIL_HOST_USER: ${EMAIL_HOST_USER:-}
EMAIL_HOST_PASSWORD: ${EMAIL_HOST_PASSWORD:-}
DEFAULT_FROM_EMAIL: ${DEFAULT_FROM_EMAIL:-}
# Cloudflare Turnstile
TURNSTILE_SECRET_KEY: ${TURNSTILE_SECRET_KEY:-}
volumes:
- static_files:/app/staticfiles
- media_files:/app/media
networks:
- internal
# Resource limits for production stability
deploy:
resources:
limits:
cpus: '2'
memory: 2G
reservations:
memory: 512M
command: >
sh -c "python manage.py collectstatic --noinput &&
python manage.py migrate --noinput &&
gunicorn --bind 0.0.0.0:8000
--workers ${GUNICORN_WORKERS:-3}
--threads 2
--worker-class gthread
--worker-tmp-dir /dev/shm
--access-logfile -
--error-logfile -
--capture-output
core.wsgi:application"
# Security: No exposed ports - internal network only
# ===========================================================================
# Nginx Frontend (SSL Termination + Static Files)
# ===========================================================================
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.prod
container_name: accountsafe-frontend
restart: always
depends_on:
- backend
ports:
- "80:80"
- "443:443"
volumes:
# Nginx configuration
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
# SSL certificates (Let's Encrypt)
- ./certbot/conf:/etc/letsencrypt:ro
- ./certbot/www:/var/www/certbot:ro
# Django static/media files
- static_files:/app/staticfiles:ro
- media_files:/app/media:ro
networks:
- internal
- external
# Resource limits for production stability
deploy:
resources:
limits:
cpus: '1'
memory: 512M
reservations:
memory: 128M
command: '/bin/sh -c ''while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g "daemon off;"'''
# ===========================================================================
# Certbot (SSL Certificate Management)
# ===========================================================================
certbot:
image: certbot/certbot:latest
container_name: accountsafe-certbot
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
# Renew certificates every 12 hours
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew --quiet; sleep 12h & wait $${!}; done;'"
networks:
- external
# Resource limits for production stability
deploy:
resources:
limits:
cpus: '0.5'
memory: 256M
reservations:
memory: 64M
# ===========================================================================
# Automated Database Backup (The Doomsday Protocol)
# ===========================================================================
# Schedule: Every 6 hours | Retention: 7 days | Format: gzip compressed SQL
# Backups are stored in ./backups/ for easy sync to S3/GCS/Azure Blob
#
# Optional Encryption: Set BACKUP_ENCRYPTION_KEY env var to GPG-encrypt dumps
# ===========================================================================
backup:
image: prodrigestivill/postgres-backup-local:15-alpine
container_name: accountsafe-backup
restart: always
depends_on:
db:
condition: service_healthy
environment:
# Database connection
POSTGRES_HOST: db
POSTGRES_DB: ${DB_NAME:-accountsafe}
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_PASSWORD: ${DB_PASSWORD:?Database password required}
# Backup schedule (cron format) - Every 6 hours
SCHEDULE: "0 */6 * * *"
# Retention policy
BACKUP_KEEP_DAYS: 7 # Delete backups older than 7 days
BACKUP_KEEP_WEEKS: 4 # Keep 4 weekly backups
BACKUP_KEEP_MONTHS: 3 # Keep 3 monthly backups
# Backup settings
BACKUP_SUFFIX: .sql.gz
HEALTHCHECK_PORT: 8080
# Optional: GPG encryption (uncomment and set in .env)
# ENCRYPT: "true"
# BACKUP_ENCRYPTION_KEY: ${BACKUP_ENCRYPTION_KEY:-}
volumes:
- ./backups:/backups
networks:
- internal
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/"]
interval: 60s
timeout: 5s
retries: 3
start_period: 30s
deploy:
resources:
limits:
cpus: '0.5'
memory: 256M
reservations:
memory: 64M
# =============================================================================
# Networks
# =============================================================================
networks:
internal:
driver: bridge
internal: true # No external access
external:
driver: bridge
# =============================================================================
# Volumes (Persistent Data)
# =============================================================================
volumes:
postgres_data:
driver: local
static_files:
driver: local
media_files:
driver: local