-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-compose.yml
More file actions
98 lines (90 loc) · 2.94 KB
/
docker-compose.yml
File metadata and controls
98 lines (90 loc) · 2.94 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
networks:
local-4eum-network:
name: local-4eum-network
driver: bridge
services:
postgres-primary:
image: postgres:15
container_name: local-postgres-primary
restart: unless-stopped
ports:
- "5433:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER:-app_user}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-app_password}
POSTGRES_DB: ${POSTGRES_DB:-app_db}
command:
- bash
- -c
- |
# init 스크립트를 컨테이너 내부에 직접 생성 (Windows CRLF 문제 회피)
printf '%s\n' '#!/bin/bash' 'echo "host replication all 0.0.0.0/0 md5" >> "$$PGDATA/pg_hba.conf"' \
> /docker-entrypoint-initdb.d/00-init-hba.sh
chmod +x /docker-entrypoint-initdb.d/00-init-hba.sh
cat > /docker-entrypoint-initdb.d/01-init-replication.sql << 'EOF'
ALTER USER app_user REPLICATION;
SELECT pg_create_physical_replication_slot('replica_slot')
WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'replica_slot');
EOF
exec docker-entrypoint.sh postgres \
-c wal_level=replica \
-c max_wal_senders=10 \
-c max_replication_slots=10 \
-c hot_standby=on
volumes:
- primary-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-app_user} -d ${POSTGRES_DB:-app_db}"]
interval: 5s
timeout: 5s
retries: 10
start_period: 10s
networks:
- local-4eum-network
postgres-replica:
image: postgres:15
container_name: local-postgres-replica
restart: unless-stopped
ports:
- "5434:5432"
depends_on:
postgres-primary:
condition: service_healthy
environment:
POSTGRES_USER: ${POSTGRES_USER:-app_user}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-app_password}
PGPASSWORD: ${POSTGRES_PASSWORD:-app_password}
command:
- bash
- -c
- |
# 기존 데이터 정리
rm -rf /var/lib/postgresql/data/*
# Primary가 replication 준비될 때까지 대기 (최대 60초)
echo "Waiting for primary to be ready for replication..."
until pg_basebackup \
--pgdata=/var/lib/postgresql/data \
--write-recovery-conf \
--slot=replica_slot \
--host=postgres-primary \
--port=5432 \
--username=$$POSTGRES_USER
do
echo "Waiting for primary..."
sleep 3
done
# 데이터 디렉토리가 비어있으면 실패
if [ ! -f /var/lib/postgresql/data/PG_VERSION ]; then
echo "ERROR: Failed to create base backup"
exit 1
fi
chown -R postgres:postgres /var/lib/postgresql/data
chmod 700 /var/lib/postgresql/data
exec gosu postgres postgres
volumes:
- replica-data:/var/lib/postgresql/data
networks:
- local-4eum-network
volumes:
primary-data:
replica-data: