Repository navigation
Expand file tree
/
Copy pathdocker-compose.yml
More file actions
225 lines (215 loc) · 6.94 KB
/
Copy pathdocker-compose.yml
File metadata and controls
225 lines (215 loc) · 6.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
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
services:
# PostgreSQL Database
db:
image: postgres:15-alpine@sha256:54e0dd8e8cddcdbb9156c2486eed5a43eccb7214d7c6828097d41ce049fb3984
environment:
POSTGRES_DB: learning_app
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
volumes:
- postgres_data:/var/lib/postgresql/data
- ./backend/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
ports:
- "${DB_PORT:-5432}:5432"
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 10
start_period: 10s
# Redis Cache
redis:
image: redis:7-alpine@sha256:6ba19c50b56777f5c11c58eff8ddb69d0d5f27ba131cffb63f180ccfd43c66af
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
networks:
- app-network
command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
# Django Backend
backend:
build:
context: ./backend
dockerfile: Dockerfile
target: dev
env_file:
- path: ./backend/.env
required: false
environment:
- DB_HOST=db
- REDIS_URL=redis://redis:6379/0
- RATELIMIT_ENABLE # パススルー: ホスト env に RATELIMIT_ENABLE があれば反映。なければデフォルト(True)
- ALLOWED_HOSTS=localhost,127.0.0.1,backend # proxy の changeOrigin: true が Host: backend:8000 を設定するため。env_file (.env) より environment が優先されるため .env の ALLOWED_HOSTS 設定に依存しない
volumes:
- ./backend:/app
- ./docs:/app/docs
- backend_static:/app/staticfiles
- backend_logs:/app/logs # named volume: bind mount では django ユーザーが /app/logs を作成できないため
ports:
- "${BACKEND_PORT:-8000}:8000"
depends_on:
db:
condition: service_healthy # Postgres 接続受付後にのみ起動(migrate の前提)
redis:
condition: service_started
networks:
- app-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health/"]
interval: 10s
timeout: 10s
retries: 18
start_period: 60s
command: >
sh -c "python manage.py collectstatic --noinput &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
# Celery Worker
celery:
build:
context: ./backend
dockerfile: Dockerfile
target: dev
env_file:
- path: ./backend/.env
required: false
environment:
- DB_HOST=db
- REDIS_URL=redis://redis:6379/0
volumes:
- ./backend:/app
depends_on:
- db
- redis
networks:
- app-network
command: celery -A core worker -l info
# Celery Beat (Scheduler)
celery-beat:
build:
context: ./backend
dockerfile: Dockerfile
target: dev
env_file:
- path: ./backend/.env
required: false
environment:
- DB_HOST=db
- REDIS_URL=redis://redis:6379/0
volumes:
- ./backend:/app
depends_on:
- db
- redis
networks:
- app-network
command: celery -A core beat -l info
# React Frontend (Development)
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.dev
environment:
- REACT_APP_API_BASE_URL=
- BACKEND_URL=http://backend:8000
- CHOKIDAR_USEPOLLING=true
- WATCHPACK_POLLING=true
volumes:
- ./frontend:/app
- /app/node_modules
ports:
- "${FRONTEND_PORT:-3000}:3000"
depends_on:
- backend
networks:
- app-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
interval: 10s
timeout: 5s
retries: 18
start_period: 60s
# E2E Init Container: backend イメージで migrate・seed_e2e を実行してから e2e サービスに引き渡す
e2e-init:
build:
context: ./backend
dockerfile: Dockerfile
target: dev
env_file:
- path: ./backend/.env
required: false
- path: ./e2e/.env.e2e # E2E_TEST_PASSWORD を env_file で読み込む(required: false で CI 環境でも動作)
required: false
environment:
- DB_HOST=db
- REDIS_URL=redis://redis:6379/0
volumes:
- ./backend:/app
depends_on:
backend:
condition: service_healthy # backend(migrate 完了)が healthy になってから実行
networks:
- app-network
profiles:
- e2e
command: >
sh -c "[ -z \"$$E2E_TEST_PASSWORD\" ] && echo 'E2E_TEST_PASSWORD is not set.' >&2 && exit 1;
python manage.py migrate --noinput &&
python manage.py seed_e2e --scenario tenant_isolation --password $$E2E_TEST_PASSWORD &&
python manage.py seed_e2e --scenario quiz_session --password $$E2E_TEST_PASSWORD"
# E2E Tests (Playwright): e2e-init 完了後に起動する
e2e:
image: mcr.microsoft.com/playwright:v1.59.1-jammy@sha256:8a0360d39d1973be506dd59002904a774f6d697d4946c94063b3fd006461c8ff
working_dir: /e2e
volumes:
- ./e2e:/e2e
- e2e_node_modules:/e2e/node_modules # named volume: Linux 向け node_modules を --rm 後も保持
depends_on:
frontend:
condition: service_started
e2e-init:
condition: service_completed_successfully # Init Container 完了後に実行
networks:
- app-network
env_file:
- path: ./e2e/.env.e2e # E2E_TEST_PASSWORD を読み込む(required: false で未作成でも起動可)
required: false
environment:
- BASE_URL=http://frontend:3000
profiles:
- e2e
# npm install: named volume のキャッシュを活かし差分のみ更新(2回目以降は数秒)。CI では e2e.yml が npm ci を使い決定論的インストールを保証
# npx playwright install chromium: イメージと @playwright/test の版がずれても、その版のブラウザを入れてから流す(版が同じなら何もしない。ずれている間は実行のたびに取り直す〔約 150MB〕が、Dependabot の次のイメージの PR でそろう・I213)
command: sh -c "npm install && npx playwright install chromium && npm test"
# Nginx (Production only)
nginx:
image: nginx:alpine@sha256:785835ea65db4bba6f021acc0f3be91b0fef76a53fe41f8588c6698d2aed0606
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- backend_static:/var/www/static
- backend_media:/var/www/media
- frontend_build:/var/www/html
depends_on:
- backend
networks:
- app-network
profiles:
- production
volumes:
postgres_data:
redis_data:
backend_media:
backend_static:
frontend_build:
backend_logs:
e2e_node_modules: # named volume: Playwright Docker イメージ(Linux)向け node_modules を保持
networks:
app-network:
driver: bridge