From e53d5d9cc1fd9f420a9f3bbb1a0c79b154750a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 19 Aug 2022 12:44:34 +0200 Subject: [PATCH] [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 --- .../compose/withPostgres/docker-compose.yml | 21 +++-- docker/compose/withPostgresAndWorker/.env | 9 +++ .../compose/withPostgresAndWorker/README.md | 26 +++++++ .../withPostgresAndWorker/docker-compose.yml | 77 +++++++++++++++++++ .../withPostgresAndWorker/init-data.sh | 12 +++ 5 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 docker/compose/withPostgresAndWorker/.env create mode 100644 docker/compose/withPostgresAndWorker/README.md create mode 100644 docker/compose/withPostgresAndWorker/docker-compose.yml create mode 100755 docker/compose/withPostgresAndWorker/init-data.sh diff --git a/docker/compose/withPostgres/docker-compose.yml b/docker/compose/withPostgres/docker-compose.yml index 39270dcd26..7ebbcb6e9f 100644 --- a/docker/compose/withPostgres/docker-compose.yml +++ b/docker/compose/withPostgres/docker-compose.yml @@ -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 diff --git a/docker/compose/withPostgresAndWorker/.env b/docker/compose/withPostgresAndWorker/.env new file mode 100644 index 0000000000..c074f42c9e --- /dev/null +++ b/docker/compose/withPostgresAndWorker/.env @@ -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 diff --git a/docker/compose/withPostgresAndWorker/README.md b/docker/compose/withPostgresAndWorker/README.md new file mode 100644 index 0000000000..766ec6c14f --- /dev/null +++ b/docker/compose/withPostgresAndWorker/README.md @@ -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. diff --git a/docker/compose/withPostgresAndWorker/docker-compose.yml b/docker/compose/withPostgresAndWorker/docker-compose.yml new file mode 100644 index 0000000000..9b334d29c9 --- /dev/null +++ b/docker/compose/withPostgresAndWorker/docker-compose.yml @@ -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 diff --git a/docker/compose/withPostgresAndWorker/init-data.sh b/docker/compose/withPostgresAndWorker/init-data.sh new file mode 100755 index 0000000000..69a8d79e1e --- /dev/null +++ b/docker/compose/withPostgresAndWorker/init-data.sh @@ -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