[N8N-4265] Improve docker compose setup with postgres (#3369)

* persist postgres data using docker volumes

* persist n8n data using docker volumes instead of writing to the host filesystem

* wait for postgres to be actually ready before starting the server

* run web server and workers as separate containers

* create a new `withPostgresAndWorker` example
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2022-08-19 12:44:34 +02:00 committed by GitHub
parent 6c9486926b
commit e53d5d9cc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 5 deletions

View file

@ -1,4 +1,8 @@
version: '3.1'
version: '3.8'
volumes:
db_storage:
n8n_storage:
services:
@ -12,7 +16,13 @@ services:
- POSTGRES_NON_ROOT_USER
- POSTGRES_NON_ROOT_PASSWORD
volumes:
- db_storage:/var/lib/postgresql/data
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
healthcheck:
test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n
@ -32,7 +42,8 @@ services:
links:
- postgres
volumes:
- ~/.n8n:/home/node/.n8n
# Wait 5 seconds to start n8n to make sure that PostgreSQL is ready
# when n8n tries to connect to it
command: /bin/sh -c "sleep 5; n8n start"
- n8n_storage:/home/node/
command: /bin/sh -c "n8n start --tunnel"
depends_on:
postgres:
condition: service_healthy

View file

@ -0,0 +1,9 @@
POSTGRES_USER=changeUser
POSTGRES_PASSWORD=changePassword
POSTGRES_DB=n8n
POSTGRES_NON_ROOT_USER=changeUser
POSTGRES_NON_ROOT_PASSWORD=changePassword
N8N_BASIC_AUTH_USER=changeUser
N8N_BASIC_AUTH_PASSWORD=changePassword

View file

@ -0,0 +1,26 @@
# n8n with PostgreSQL and Worker
Starts n8n with PostgreSQL as database, and the Worker as a separate container.
## Start
To start n8n simply start docker-compose by executing the following
command in the current folder.
**IMPORTANT:** But before you do that change the default users and passwords in the [`.env`](.env) file!
```
docker-compose up -d
```
To stop it execute:
```
docker-compose stop
```
## Configuration
The default name of the database, user and password for PostgreSQL can be changed in the [`.env`](.env) file in the current directory.

View file

@ -0,0 +1,77 @@
version: '3.8'
volumes:
db_storage:
n8n_storage:
redis_storage:
x-shared: &shared
restart: always
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
- EXECUTIONS_MODE=queue
- QUEUE_BULL_REDIS_HOST=redis
- QUEUE_HEALTH_CHECK_ACTIVE=true
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER
- N8N_BASIC_AUTH_PASSWORD
links:
- postgres
- redis
volumes:
- n8n_storage:/home/node/
depends_on:
redis:
condition: service_healthy
postgres:
condition: service_healthy
services:
postgres:
image: postgres:11
restart: always
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
- POSTGRES_NON_ROOT_USER
- POSTGRES_NON_ROOT_PASSWORD
volumes:
- db_storage:/var/lib/postgresql/data
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
healthcheck:
test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
redis:
image: redis:6-alpine
restart: always
volumes:
- redis_storage:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 10
n8n:
<<: *shared
image: n8nio/n8n
command: /bin/sh -c "n8n start --tunnel"
ports:
- 5678:5678
n8n-worker:
<<: *shared
image: n8nio/n8n
command: /bin/sh -c "sleep 5; n8n worker"
depends_on:
- n8n

View file

@ -0,0 +1,12 @@
#!/bin/bash
set -e;
if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
EOSQL
else
echo "SETUP INFO: No Environment variables given!"
fi