-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-compose.yaml
More file actions
173 lines (162 loc) · 5.6 KB
/
docker-compose.yaml
File metadata and controls
173 lines (162 loc) · 5.6 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
services:
postgres:
build:
context: .
dockerfile: postgres/Dockerfile
container_name: postgres-prod
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER:-dbuser}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Please set POSTGRES_PASSWORD}
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --auth-local=scram-sha-256"
# Use a subdirectory for PGDATA so first-init works even if the host mount
# contains system entries like lost+found
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init-scripts:/docker-entrypoint-initdb.d:ro
- ./postgresql.conf:/etc/postgresql/postgresql.conf:ro
- ./pg_hba.conf:/etc/postgresql/pg_hba.conf:ro
- ./certs:/etc/postgresql/certs:ro
depends_on: {}
command: >
postgres
-c config_file=/etc/postgresql/postgresql.conf
-c hba_file=/etc/postgresql/pg_hba.conf
healthcheck:
test:
[
"CMD-SHELL",
"pg_isready -U ${POSTGRES_USER:-dbuser} -d ${POSTGRES_DATABASE:-postgres}",
]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
# Automated backup service
postgres-backup:
build:
context: .
dockerfile: backup/Dockerfile
container_name: postgres-backup
restart: unless-stopped
environment:
# Schedule - daily at 2 AM
SCHEDULE: "0 2 * * *"
# Database connection (internal network)
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DATABASE: ${POSTGRES_DATABASE:-postgres}
POSTGRES_USER: ${BACKUP_USER:-admin}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_EXTRA_OPTS: "--schema=public --blobs --compress=9"
# Backup scope: 'database' (single DB) or 'cluster' (all DBs)
BACKUP_SCOPE: ${BACKUP_SCOPE:-database}
# TLS client certs for mTLS to Postgres
PGSSLMODE: ${PGSSLMODE:-verify-full}
PGSSLROOTCERT: ${PGSSLROOTCERT:-/client-certs/ca.crt}
PGSSLCERT: ${PGSSLCERT:-/client-certs/admin/client.crt}
PGSSLKEY: ${PGSSLKEY:-/client-certs/admin/client.key}
# S3 configuration
S3_ACCESS_KEY_ID: ${HETZNER_S3_ACCESS_KEY}
S3_SECRET_ACCESS_KEY: ${HETZNER_S3_SECRET_KEY}
S3_BUCKET: ${HETZNER_S3_BUCKET:-postgres-backups}
S3_REGION: ${HETZNER_S3_REGION:-eu-central}
S3_ENDPOINT: ${HETZNER_S3_ENDPOINT}
S3_PREFIX: backup
# Backup retention (days)
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-30}
# Backup types
WEEKLY_BACKUP: "yes"
MONTHLY_BACKUP: "yes"
volumes:
- ./client-certs:/client-certs:ro
- ./certs:/certs:ro
depends_on:
postgres:
condition: service_healthy
profiles:
- backup
# One-time restore service
postgres-restore:
build:
context: .
dockerfile: restore/Dockerfile
container_name: postgres-restore
restart: "no"
environment:
# Database connection
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_DATABASE: ${POSTGRES_DATABASE:-postgres}
POSTGRES_USER: ${RESTORE_USER:-admin}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
# TLS client certs for mTLS to Postgres
PGSSLMODE: ${PGSSLMODE:-verify-full}
PGSSLROOTCERT: ${PGSSLROOTCERT:-/client-certs/ca.crt}
PGSSLCERT: ${PGSSLCERT:-/client-certs/admin/client.crt}
PGSSLKEY: ${PGSSLKEY:-/client-certs/admin/client.key}
# S3 configuration
S3_ACCESS_KEY_ID: ${HETZNER_S3_ACCESS_KEY}
S3_SECRET_ACCESS_KEY: ${HETZNER_S3_SECRET_KEY}
S3_BUCKET: ${HETZNER_S3_BUCKET:-postgres-backups}
S3_REGION: ${HETZNER_S3_REGION:-eu-central}
S3_ENDPOINT: ${HETZNER_S3_ENDPOINT}
S3_PREFIX: backup
# Restore options
DROP_PUBLIC: "yes"
BACKUP_FILE: ${RESTORE_BACKUP_FILE:-latest}
volumes:
- ./client-certs:/client-certs:ro
- ./certs:/certs:ro
depends_on:
postgres:
condition: service_healthy
profiles:
- restore
# PostgreSQL monitoring
postgres-exporter:
image: prometheuscommunity/postgres-exporter:latest
container_name: postgres-exporter
restart: unless-stopped
environment:
DATA_SOURCE_NAME: "postgresql://${POSTGRES_USER:-dbuser}@${POSTGRES_HOST:-postgres}:${POSTGRES_PORT:-5432}/${POSTGRES_DATABASE:-postgres}?sslmode=${PGSSLMODE:-verify-full}&sslrootcert=${PGSSLROOTCERT:-/client-certs/ca.crt}&sslcert=${PGSSLCERT:-/client-certs/client.crt}&sslkey=${PGSSLKEY:-/client-certs/client.key}"
ports:
- "9187:9187"
depends_on:
postgres:
condition: service_healthy
profiles:
- monitoring
volumes:
- ./client-certs:/client-certs:ro
# Cloudflare Tunnel (optional)
cloudflared:
image: cloudflare/cloudflared:latest
container_name: cloudflared
restart: unless-stopped
# Use a dashboard-managed tunnel token (recommended). Create the tunnel in
# Cloudflare Zero Trust, then copy the token here.
command: tunnel --no-autoupdate run --token ${CLOUDFLARE_TUNNEL_TOKEN}
environment:
# Optional: if you prefer to run with a local config file instead of a token,
# mount ./cloudflared and set a custom command (see docs/cloudflare-tunnel.md)
- TUNNEL_ORIGIN=postgres:5432
depends_on:
postgres:
condition: service_started
profiles:
- tunnel
# Mount a config dir if you want to use a local config.yml/credentials
volumes:
- ./cloudflared:/etc/cloudflared
volumes:
postgres_data:
driver: local