-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-compose.postgres.yml
More file actions
206 lines (199 loc) · 5.04 KB
/
docker-compose.postgres.yml
File metadata and controls
206 lines (199 loc) · 5.04 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
version: '3.8'
services:
# VerTree 主应用
vertree-app:
build:
context: .
dockerfile: Dockerfile
args:
- ENVIRONMENT=production
container_name: vertree-postgres-app
ports:
- "${SERVER_PORT:-8080}:8080"
env_file:
- postgres.env
volumes:
- ./uploads:/root/uploads:rw
- ./config:/root/config:ro
- app_logs:/root/logs
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 5
start_period: 60s
restart: unless-stopped
networks:
- vertree-network
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M
# PostgreSQL 数据库
postgres:
image: postgres:15-alpine
container_name: vertree-postgres-db
env_file:
- postgres.env
volumes:
- postgres_data:/var/lib/postgresql/data
- ./migrations:/docker-entrypoint-initdb.d:ro
- postgres_logs:/var/log/postgresql
ports:
- "${POSTGRES_PORT:-5432}:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-vertree_user} -d ${POSTGRES_DB:-vertree}"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
restart: unless-stopped
networks:
- vertree-network
deploy:
resources:
limits:
cpus: '1.0'
memory: 1G
reservations:
cpus: '0.25'
memory: 256M
command: >
postgres
-c shared_preload_libraries=pg_stat_statements
-c pg_stat_statements.track=all
-c max_connections=200
-c shared_buffers=256MB
-c effective_cache_size=1GB
-c work_mem=4MB
-c maintenance_work_mem=64MB
-c checkpoint_completion_target=0.7
-c wal_buffers=16MB
-c default_statistics_target=100
-c random_page_cost=1.1
-c effective_io_concurrency=200
-c log_statement=mod
-c log_duration=on
-c log_line_prefix='%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
# Redis 缓存
redis:
image: redis:7-alpine
container_name: vertree-redis
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
- redis_logs:/var/log/redis
command: >
redis-server
--appendonly yes
--appendfsync everysec
--auto-aof-rewrite-percentage 100
--auto-aof-rewrite-min-size 64mb
--maxmemory 256mb
--maxmemory-policy allkeys-lru
--requirepass ${REDIS_PASSWORD:-redis_password_change_me}
healthcheck:
test: ["CMD", "redis-cli", "--no-auth-warning", "-a", "${REDIS_PASSWORD:-redis_password_change_me}", "ping"]
interval: 30s
timeout: 10s
retries: 3
restart: unless-stopped
networks:
- vertree-network
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
reservations:
cpus: '0.1'
memory: 64M
# Nginx 反向代理
nginx:
image: nginx:alpine
container_name: vertree-nginx
ports:
- "${NGINX_HTTP_PORT:-80}:80"
- "${NGINX_HTTPS_PORT:-443}:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
- nginx_logs:/var/log/nginx
- nginx_cache:/var/cache/nginx
depends_on:
vertree-app:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
interval: 30s
timeout: 10s
retries: 3
restart: unless-stopped
networks:
- vertree-network
deploy:
resources:
limits:
cpus: '0.5'
memory: 256M
reservations:
cpus: '0.1'
memory: 32M
# PostgreSQL 备份服务 (可选)
postgres-backup:
image: postgres:15-alpine
container_name: vertree-postgres-backup
env_file:
- postgres.env
volumes:
- ./backups:/backups
- postgres_backup_scripts:/scripts:ro
depends_on:
postgres:
condition: service_healthy
command: >
sh -c "
echo '#!/bin/sh' > /scripts/backup.sh &&
echo 'pg_dump -h postgres -U $$POSTGRES_USER $$POSTGRES_DB | gzip > /backups/backup_$$(date +%Y%m%d_%H%M%S).sql.gz' >> /scripts/backup.sh &&
echo 'find /backups -name \"backup_*.sql.gz\" -mtime +7 -delete' >> /scripts/backup.sh &&
chmod +x /scripts/backup.sh &&
crond -f
"
restart: unless-stopped
networks:
- vertree-network
profiles:
- backup
volumes:
postgres_data:
driver: local
redis_data:
driver: local
nginx_logs:
driver: local
nginx_cache:
driver: local
app_logs:
driver: local
postgres_logs:
driver: local
redis_logs:
driver: local
postgres_backup_scripts:
driver: local
networks:
vertree-network:
driver: bridge
ipam:
config:
- subnet: 172.21.0.0/16