-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.yaml
339 lines (333 loc) · 8.43 KB
/
base.yaml
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
services:
postgresql:
image: postgres:17.0-alpine
user: postgres
environment:
# We have to redefine POSTGRES_DB and POSTGRES_USER as themselves due to this: https://docs.docker.com/compose/environment-variables/envvars-precedence/
POSTGRES_DB: $PROJECT
POSTGRES_USER: $PROJECT
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_INITDB_ARGS: --locale-provider=icu --icu-locale=$LOCALE
LC_COLLATE: $LANG
LC_CTYPE: $LANG
LC_MESSAGES: $LANG
LC_MONETARY: $LANG
LC_NUMERIC: $LANG
LC_TIME: $LANG
TZ: $TZ
volumes:
- postgresql:/var/lib/postgresql/data
- postgresql-socket:/var/run/postgresql
network_mode: none
read_only: true
tmpfs:
- /tmp/
- /run/postgresql/
healthcheck:
test: [ "CMD", "pg_isready", "-U", $PROJECT ]
start_period: 60s
start_interval: 1s
interval: 60s
timeout: 60s
retries: 3
restart: unless-stopped
cap_drop:
- ALL
cap_add:
- CHOWN
- DAC_READ_SEARCH
- FOWNER
- SETGID
- SETUID
deploy:
mode: global
resources:
limits:
memory: 200M
reservations:
memory: 50M
logging: &limited_logging
options:
max-file: "3"
max-size: "10m"
migrate:
image: $PROJECT/django:${IMAGES_TAG:-latest}
command: sh -c "./manage.py collectstatic ${DJANGO_COLLECTSTATIC_ARGS} --no-input --clear && ./manage.py migrate"
environment: &django_environment
PROJECT: $PROJECT
PROJECT_VERBOSE: $PROJECT_VERBOSE
DOMAIN: $DOMAIN
TZ: $TZ
LOCALE: $LOCALE
LANGUAGES_CODES: $LANGUAGES_CODES
CLIENT_MAX_BODY_SIZE: ${CLIENT_MAX_BODY_SIZE}
secrets:
- .env.secrets
volumes: &django_volumes
- postgresql-socket:/var/run/postgresql
- media:/srv/media
- static:/srv/static
networks:
internal:
read_only: true
tmpfs:
- /tmp/
restart: "no"
depends_on:
postgresql:
condition: service_healthy
cap_drop:
- ALL
deploy:
resources:
limits:
memory: 500M
reservations:
memory: 150M
logging: *limited_logging
django:
build:
context: .
args: &django-build-args
DJANGO_BASE_IMAGE: ${DJANGO_BASE_IMAGE:-python:3.12.6-slim-bookworm}
DJANGO_PRE_INSTALL_RUN: ${DJANGO_PRE_INSTALL_RUN:-}
DJANGO_APT_DEPENDENCIES: ${DJANGO_APT_DEPENDENCIES:-}
DJANGO_NODE_BUILD: ${DJANGO_NODE_BUILD:-django_without_node}
DJANGO_ROOT: ${DJANGO_ROOT}
DJANGO_EXTRA_PIP_ARGS: ${DJANGO_EXTRA_PIP_ARGS:-}
DJANGO_POST_INSTALL_RUN: ${DJANGO_POST_INSTALL_RUN:-}
DJANGO_ENVIRONMENT: $DJANGO_ENVIRONMENT
DJANGO_CPUS: ${DJANGO_CPUS:-1}
secrets:
- .env.secrets
image: $PROJECT/django:${IMAGES_TAG:-latest}
environment: *django_environment
secrets:
- .env.secrets
volumes: *django_volumes
networks:
internal:
read_only: true
tmpfs:
- /tmp/
expose:
- 8000
healthcheck:
test: python3 -c "import urllib.request; urllib.request.urlopen('http://django:8000${DJANGO_HEALTHCHECK_URL:-/}')"
start_period: 60s
start_interval: 1s
interval: 60s
timeout: 60s
retries: 3
restart: unless-stopped
depends_on:
postgresql:
condition: service_healthy
migrate:
condition: service_completed_successfully
cap_drop:
- ALL
deploy:
resources:
limits:
cpus: "${DJANGO_CPUS:-1}"
memory: 500M
reservations:
memory: 150M
logging: *limited_logging
nginx:
build:
context: .
dockerfile: nginx.Dockerfile
args:
DOMAIN: $DOMAIN
NGINX_ROOT: ${NGINX_ROOT}
DJANGO_ROOT: ${DJANGO_ROOT}
CLIENT_MAX_BODY_SIZE: ${CLIENT_MAX_BODY_SIZE}
image: $PROJECT/nginx:${IMAGES_TAG:-latest}
environment:
# We have to redefine some variables as themselves due to this: https://docs.docker.com/compose/environment-variables/envvars-precedence/
ALTERNATE_DOMAINS_NGINX: $ALTERNATE_DOMAINS_NGINX
IPV4_SUBNET: $IPV4_SUBNET
IPV6_SUBNET: $IPV6_SUBNET
HSTS_MAX_AGE: 86400
CSP_DEFAULT: "'self'"
CSP_SCRIPT: "'self' blob: 'unsafe-inline' 'unsafe-eval'"
CSP_IMG: "'self' data: blob:"
CSP_STYLE: "'self' 'unsafe-inline'"
CSP_FONT: "'self' data:"
CSP_CONNECT: "'self'"
CSP_FRAME: "'self'"
CSP_FRAME_ANCESTORS: "'self'"
CSP_OBJECT: "'self'"
PROXY_READ_TIMEOUT: 21600s
volumes:
- media:/srv/media:ro
- static:/srv/static:ro
- nginx-logs:/var/log/nginx
networks:
internal:
traefik:
read_only: true
tmpfs:
- /var/cache/nginx/
- /var/run/
- /etc/nginx/conf.d/
healthcheck:
test: nc -vz -w1 nginx 80
start_period: 60s
start_interval: 1s
interval: 60s
timeout: 60s
retries: 3
restart: unless-stopped
depends_on:
django:
condition: service_healthy
expose:
- 80
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
- CHOWN
- SETGID
- SETUID
deploy:
resources:
limits:
cpus: "1"
memory: 80M
reservations:
memory: 6M
labels:
- traefik.enable=true
- traefik.docker.network=traefik
- traefik.http.routers.$PROJECT-nginx.rule=Host(`$DOMAIN`) || $ALTERNATE_DOMAINS_TRAEFIK
logging: *limited_logging
traefik:
image: traefik:3.2.0
container_name: traefik
environment:
TZ: $TZ
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
traefik:
read_only: true
healthcheck:
test: nc -z -w1 traefik 80
start_period: 60s
start_interval: 1s
interval: 60s
timeout: 60s
retries: 3
restart: unless-stopped
depends_on:
nginx:
condition: service_healthy
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
deploy:
resources:
limits:
cpus: "1"
memory: 200M
reservations:
memory: 40M
logging: *limited_logging
fail2ban:
build:
context: fail2ban
args:
PROJECT: $PROJECT
TZ: $TZ
HOST_HAS_SSH: $HOST_HAS_SSH
image: $PROJECT/fail2ban:${IMAGES_TAG:-latest}
container_name: fail2ban
network_mode: host
volumes:
- fail2ban:/var/lib/fail2ban
- /var/log/auth.log:/var/log/auth.log:ro
- nginx-logs:/var/log/nginx:ro
# TODO: Make the container read-only
healthcheck:
test: [ "CMD", "fail2ban-client", "ping" ]
start_period: 60s
start_interval: 1s
interval: 60s
timeout: 60s
retries: 3
restart: unless-stopped
depends_on:
traefik:
condition: service_healthy
cap_drop:
- ALL
cap_add:
- DAC_OVERRIDE
- NET_ADMIN
- NET_RAW
deploy:
resources:
limits:
cpus: "1"
memory: 100M
reservations:
memory: 20M
logging: *limited_logging
ofelia:
build:
context: ofelia
image: $PROJECT/ofelia:${IMAGES_TAG:-latest}
command: daemon --docker -f label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}
environment:
TZ: $TZ
network_mode: none
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
read_only: true
# TODO: Add a health check.
restart: unless-stopped
depends_on:
traefik:
condition: service_healthy
cap_drop:
- ALL
deploy:
resources:
limits:
cpus: "1"
memory: 150M
reservations:
memory: 50M
labels:
- ofelia.job-local.$PROJECT-restart-unhealthy-containers.schedule=@every 60s
- ofelia.job-local.$PROJECT-restart-unhealthy-containers.command=sh -c 'docker ps -q -f health=unhealthy -f name=${COMPOSE_PROJECT_NAME}- | xargs --no-run-if-empty docker restart'
- ofelia.job-local.$PROJECT-restart-unhealthy-containers.no-overlap=true
logging: *limited_logging
volumes:
postgresql:
postgresql-socket:
media:
static:
nginx-logs:
name: "nginx-logs" # That way, no project prefix.
fail2ban:
networks:
internal:
enable_ipv6: true
traefik:
enable_ipv6: true
name: "traefik"
ipam:
config:
- subnet: $IPV4_SUBNET
- subnet: $IPV6_SUBNET
secrets:
.env.secrets:
file: .env.secrets