Repository navigation
Expand file tree
/
Copy pathcompose.yaml
More file actions
306 lines (296 loc) · 14.2 KB
/
Copy pathcompose.yaml
File metadata and controls
306 lines (296 loc) · 14.2 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
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
# Notifuse with its own PostgreSQL, for production and development alike.
#
# The file works on its own: a server needs no clone of the repository.
#
# curl -fsSL https://raw.githubusercontent.com/Notifuse/notifuse/main/compose.yaml -o compose.yaml
# cat > .env <<EOF
# SECRET_KEY=$(openssl rand -base64 32)
# DB_PASSWORD=$(openssl rand -hex 24)
# EOF
# docker compose up -d
#
# Then open http://<server>:8081 and finish the setup wizard. Notifuse serves plain
# HTTP: put a reverse proxy with TLS in front of it before pointing a domain at it.
#
# Commands:
# docker compose up -d start, pulling the release named below
# docker compose up -d --build from a clone: build the image from this checkout
# docker compose watch from a clone: rebuild when the source changes
# docker compose logs -f api follow the server log
# docker compose down stop; the database volume is KEPT
# docker compose down -v stop and DELETE the database (and its upgrade copy)
#
# Upgrading Notifuse: set NOTIFUSE_VERSION in .env (or take the new version of this
# file), then `docker compose up -d`. Read the CHANGELOG entries in between first.
#
# PostgreSQL 18. An installation made with PostgreSQL 17 is upgraded by the next
# `docker compose up -d`: the pg-upgrade service below first copies the 17 data to
# the postgres-upgrade-backup volume, then upgrades it in place. See
# https://docs.notifuse.com/self-hosting/upgrading-postgresql to roll back, or to
# delete the copy once you are satisfied.
#
# Backups: dump every database, the system one and one per workspace (pg_dumpall
# does not restore here), and keep SECRET_KEY with them. With another DB_USER,
# DB_NAME or DB_PREFIX, put yours in place of postgres, notifuse_system and notifuse:
# for db in $(docker compose exec -T postgres psql -U postgres -d postgres -Atc "SELECT datname FROM pg_database WHERE datname = 'notifuse_system' OR datname LIKE 'notifuse\_ws\_%'"); do
# docker compose exec -T postgres pg_dump -U postgres -Fc "$db" > "$db.dump"
# done
#
# Configuration comes from a .env file next to this one; env.example lists every
# setting. SMTP, the root account and the public endpoint can all be set in the
# setup wizard instead.
# ⚠️ In .env, values containing # must be quoted: DB_PASSWORD="mypass#word123"
#
# Requires Docker Engine 25.0 and Compose v2.24 or newer (`docker compose version`).
services:
api:
# Pin a release in .env (NOTIFUSE_VERSION=v44.0). The default below moves with
# this file, so `git pull` or a new copy of the file, then `up -d`, upgrades.
# From a clone, a tag not published yet (on the dev branch) is built from source.
image: notifuse/notifuse:${NOTIFUSE_VERSION:-v44.0}
build:
context: .
dockerfile: Dockerfile
ports:
# host:container. The container side follows SERVER_PORT: publishing a fixed
# port while the server listens on another gives a healthy container that
# nothing can reach. HTTP_BIND=127.0.0.1 in .env when a reverse proxy on this
# host terminates TLS.
# Without HTTP_BIND, every interface, IPv6 included.
- '${HTTP_BIND:+${HTTP_BIND}:}${HTTP_PORT:-8081}:${SERVER_PORT:-8080}'
# Inbound SMTP bridge, used only when SMTP_BRIDGE_ENABLED is set. The container
# side follows SMTP_BRIDGE_PORT, as the HTTP one follows SERVER_PORT.
- '${SMTP_BRIDGE_HOST_PORT:-587}:${SMTP_BRIDGE_PORT:-587}'
# Every variable in .env reaches the server (up to 43.0, only those named below
# did), so a setting not named below needs no edit to this file. The values below
# still win for the keys they name.
env_file:
- path: .env
required: false
environment:
# Server Configuration
- SERVER_PORT=${SERVER_PORT:-8080}
- SERVER_HOST=${SERVER_HOST:-0.0.0.0}
- ENVIRONMENT=${ENVIRONMENT:-production}
# Database Configuration. DB_PASSWORD is read by both services, so they cannot
# drift apart. It defaults to postgres because existing installations were
# initialised with it; a new installation sets its own in .env.
- DB_HOST=${DB_HOST:-postgres}
- DB_PORT=${DB_PORT:-5432}
- DB_USER=${DB_USER:-postgres}
- DB_PASSWORD=${DB_PASSWORD:-postgres}
- DB_PREFIX=${DB_PREFIX:-notifuse}
- DB_NAME=${DB_NAME:-notifuse_system}
- DB_SSLMODE=${DB_SSLMODE:-disable}
# Secret Key (Required) - encrypts stored credentials and signs sessions and
# API keys. Set it in .env; a new installation generates one with
# openssl rand -base64 32. To replace it, list the new key first and the
# current one after a comma: Notifuse re-encrypts the stored data with the new
# key when it starts (see the 42.0 entry of CHANGELOG.md).
- 'SECRET_KEY=${SECRET_KEY:?set SECRET_KEY in .env. New installation, generate one with openssl rand -base64 32. Before 42.0 compose.yaml provided a default, see the 42.0 entry of CHANGELOG.md}'
# Notifuse licence key (Optional). Leaving it unset is the ordinary state
# of a self-hosted installation and means the free tier. It overrides any
# key pasted in the console under Settings > Licence. See env.example for
# what a licence covers and where a key comes from.
# - NOTIFUSE_LICENSE_KEY=${NOTIFUSE_LICENSE_KEY}
# All other settings (ROOT_EMAIL, API_ENDPOINT, SMTP, the SMTP bridge...) can
# be configured through the setup wizard on first launch, or set in .env.
depends_on:
postgres:
# Waits for the healthcheck, not only for the container to exist, so the
# server does not race a database that is still initialising or upgrading.
condition: service_healthy
restart: true
volumes:
- ./data:/app/data
restart: unless-stopped
# SIGTERM starts a graceful shutdown, given 70 s by cmd/api/main.go: the automation step
# under way, the emails being sent and the web analytics buffer finish. Docker's default 10 s
# would kill it first.
stop_grace_period: 75s
security_opt:
- no-new-privileges:true
networks:
- notifuse-network
healthcheck:
# CMD-SHELL, so the port follows SERVER_PORT instead of being hardcoded.
test: ['CMD-SHELL', 'wget -q -O /dev/null http://127.0.0.1:$${SERVER_PORT:-8080}/healthz || exit 1']
interval: 30s
timeout: 5s
retries: 3
start_period: 60s
start_interval: 2s
logging: &logging
# Without a cap, container logs grow until the disk is full.
driver: json-file
options:
max-size: '10m'
max-file: '5'
develop:
# Only `docker compose watch` acts on these. Rebuilding is the only correct
# action: the image compiles Go and bundles the frontends.
watch:
- action: rebuild
path: ./cmd
- action: rebuild
path: ./config
- action: rebuild
path: ./internal
- action: rebuild
path: ./pkg
- action: rebuild
path: ./console/src
- action: rebuild
path: ./notification_center/src
- action: rebuild
path: ./web_analytics_sdk/src
# Runs to completion before PostgreSQL starts, on every `up`. On an empty volume
# or one already on 18 it exits at once. On data from an older PostgreSQL (17), it
# copies that data to the postgres-upgrade-backup volume (replacing any earlier
# copy), then upgrades it in place with pg_upgrade --link, which deletes the old
# data on the data volume.
# Without enough space for the copy it stops before changing anything, and so
# does PostgreSQL: the error is in `docker compose logs pg-upgrade`. An upgrade
# interrupted half-way starts again from the copy at the next `up`.
pg-upgrade:
# Pinned by digest: pgautoupgrade publishes no versioned releases.
image: pgautoupgrade/pgautoupgrade:18-alpine@sha256:722599f0fe6483f2a97e765066e23122a780b4ea221287edfaaf67268af6757a
entrypoint: ['sh', '/pg-upgrade.sh']
configs:
- source: pg-upgrade
target: /pg-upgrade.sh
environment:
# The role the data was created with: pgautoupgrade connects as it.
- POSTGRES_USER=${DB_USER:-postgres}
- POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}
# The database it connects to; it defaults to POSTGRES_USER, which names no
# database once DB_USER is not postgres.
- POSTGRES_DB=${POSTGRES_DB:-postgres}
- PGAUTO_ONESHOT=yes
# Skips rebuilding every index of every workspace database. Safe because the
# old and the new image are both Alpine (musl), whose text ordering cannot
# change between them. Set it to yes if you replaced the postgres image with
# a Debian one.
- PGAUTO_REINDEX=${PGAUTO_REINDEX:-no}
volumes:
- postgres-data:/var/lib/postgresql
- postgres-upgrade-backup:/backup
network_mode: none
restart: 'no'
logging: *logging
postgres:
# The image's major version and the mount path below are a matched pair:
# PostgreSQL 18 keeps its data in /var/lib/postgresql/18/docker, below the
# volume, which is what lets pg-upgrade move it to the next major in place.
# To stay on 17, use postgres:17-alpine with the volume at
# /var/lib/postgresql/data, and remove pg-upgrade. Notifuse runs on 17 or newer.
#
# Never set PGDATA here: an explicit PGDATA turns off the image's check for data
# from an older major, and PostgreSQL then initialises an empty database next
# to yours instead of refusing to start. DB_EXPECT_EXISTING=true in .env makes
# Notifuse refuse an empty database too.
image: postgres:18-alpine
ports:
# Reachable from this host only (psql, backups). Set POSTGRES_HOST_PORT in
# .env when 5433 is taken (tests/compose.test.yaml uses it too).
- '127.0.0.1:${POSTGRES_HOST_PORT:-5433}:5432'
environment:
- POSTGRES_USER=${DB_USER:-postgres}
- POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}
# Must stay a database the server can connect to before its own exists:
# Notifuse creates notifuse_system, and every workspace database, from here.
- POSTGRES_DB=${POSTGRES_DB:-postgres}
depends_on:
pg-upgrade:
condition: service_completed_successfully
# Docker gives a container 64 MB of /dev/shm, which the parallel workers behind
# the web analytics aggregations can exhaust ("could not resize shared memory
# segment").
shm_size: 256mb
volumes:
- postgres-data:/var/lib/postgresql
restart: unless-stopped
networks:
- notifuse-network
# SIGTERM is a smart shutdown in PostgreSQL: it waits for clients to disconnect,
# and Docker's default 10 s would turn that into a SIGKILL and a recovery at the
# next start.
stop_grace_period: 1m
healthcheck:
# Follows POSTGRES_USER and POSTGRES_DB rather than assuming postgres.
test: ['CMD-SHELL', 'pg_isready -U "$${POSTGRES_USER}" -d "$${POSTGRES_DB}"']
interval: 30s
timeout: 5s
retries: 5
start_period: 60s
start_interval: 1s
logging: *logging
configs:
# The pg-upgrade entrypoint, kept in this file so that it works on its own. $$ is
# a literal $ (Compose interpolates the content).
pg-upgrade:
content: |
set -eu
data=/var/lib/postgresql
backup=/backup
restored=
# An upgrade that stopped half-way leaves its lock, with the old data moved
# where pgautoupgrade no longer finds it: start again from the copy. A putting
# back that stopped half-way leaves its own mark, in the backup volume, since
# it empties the data volume lock included: start it again too.
if [ -e "$$data/18/docker/upgrade_in_progress.lock" ] || [ -e "$$backup/restore_in_progress" ]; then
copy=
for dir in "$$backup"/pg*; do
case "$$dir" in *.partial) continue ;; esac
if [ -s "$$dir/PG_VERSION" ]; then copy=$$dir; fi
done
if [ -z "$$copy" ]; then
echo "A previous upgrade to PostgreSQL 18 did not finish, and the postgres-upgrade-backup volume holds no copy to start again from. Nothing was changed: see https://docs.notifuse.com/self-hosting/upgrading-postgresql" >&2
exit 1
fi
echo "A previous upgrade to PostgreSQL 18 did not finish: putting back the data copied before it ($$copy), then upgrading again."
touch "$$backup/restore_in_progress"
find "$$data" -mindepth 1 -delete
cp -a "$$copy/." "$$data/"
rm "$$backup/restore_in_progress"
restored=1
fi
# Data from an older PostgreSQL sits at the root of the volume; 18 keeps its
# own in 18/docker. Anything else has nothing to upgrade, and pgautoupgrade is
# not started at all: it never runs beside a PostgreSQL that uses the data.
if [ ! -s "$$data/PG_VERSION" ]; then
echo "No data from an older PostgreSQL to upgrade."
exit 0
fi
old=$$(cat "$$data/PG_VERSION")
major=$${old%%.*}
if [ "$$major" -ge 18 ]; then
echo "The data is already on PostgreSQL $$old."
exit 0
fi
# A fresh copy before every upgrade, in place of any older one: after a
# rollback, the data about to be upgraded is newer than the last copy.
if [ -z "$$restored" ]; then
find "$$backup" -mindepth 1 -delete
need=$$(du -sk "$$data" | cut -f1)
free=$$(df -Pk "$$backup" | awk 'NR == 2 { print $$4 }')
if [ "$$free" -lt $$((need + need / 10)) ]; then
echo "Not enough space to copy the PostgreSQL $$old data before upgrading it: $$need KB to copy, $$free KB free on the postgres-upgrade-backup volume. Nothing was changed; free some space, then run docker compose up -d again." >&2
exit 1
fi
echo "Copying the PostgreSQL $$old data to the postgres-upgrade-backup volume (pg$$major) before upgrading it to 18."
cp -a "$$data" "$$backup/pg$$major.partial"
mv "$$backup/pg$$major.partial" "$$backup/pg$$major"
echo "Copy done. Upgrading the data to PostgreSQL 18."
fi
exec docker-entrypoint.sh postgres
volumes:
postgres-data:
driver: local
# The PostgreSQL 17 data as it was before the upgrade to 18. Delete it once the
# upgraded installation works: docker volume rm <project>_postgres-upgrade-backup
postgres-upgrade-backup:
driver: local
networks:
notifuse-network:
driver: bridge