n8n
n8n is a workflow automation tool whose AI agent nodes have made it the default way to wire a model to everything else. It stores the credentials of every service your workflows call, encrypted with one key, and that key is the thing this example is about.
1. Write the Compose file
Section titled “1. Write the Compose file”Adapted from the withPostgres setup in
n8n-hosting, without the init script
it mounts from the host:
services: postgres: image: docker.io/library/postgres:18 restart: unless-stopped environment: - POSTGRES_USER=n8n - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_DB=n8n - PGDATA=/var/lib/postgresql/data volumes: - db:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -h localhost -U n8n -d n8n"] interval: 5s timeout: 5s retries: 10
n8n: image: docker.io/n8nio/n8n:${N8N_VERSION} restart: unless-stopped environment: - DB_TYPE=postgresdb - DB_POSTGRESDB_HOST=postgres - DB_POSTGRESDB_PORT=5432 - DB_POSTGRESDB_DATABASE=n8n - DB_POSTGRESDB_USER=n8n - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD} - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY} - N8N_RUNNERS_MODE=external - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN} - N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0 - GENERIC_TIMEZONE=${TZ} - TZ=${TZ} ports: - "127.0.0.1:5678:5678" volumes: - n8n:/home/node/.n8n depends_on: postgres: condition: service_healthy
n8n-runner: image: docker.io/n8nio/runners:${N8N_VERSION} restart: unless-stopped environment: - N8N_RUNNERS_AUTH_TOKEN=${RUNNERS_AUTH_TOKEN} - N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679 depends_on: - n8n
volumes: db: n8n:- The runner executes the Code nodes in a container of its own, and must run
the same version as n8n: one
N8N_VERSIONdrives both images. - The upstream file mounts
./init-data.shto create a second database user. This one connects as the owner instead, so the stack is its Compose file and nothing beside it, and a copy has no host file to leave behind.
2. Write its variables
Section titled “2. Write its variables”N8N_VERSION=stableTZ=Europe/ParisPOSTGRES_PASSWORD=generate-with-openssl-rand-hex-32N8N_ENCRYPTION_KEY=generate-with-openssl-rand-hex-32RUNNERS_AUTH_TOKEN=generate-with-openssl-rand-hex-32All three secrets are flagged by name. N8N_ENCRYPTION_KEY is the one to
protect: n8n encrypts every stored credential with it. Left unset, n8n
generates one and writes it into /home/node/.n8n, where it lives only as
long as that volume. Set in Kaio, it is in every
version of the stack, so a rollback, a recreate or a
copy brings the same key back to the same credentials.
Losing it means re-entering every credential of every workflow. Back up Kaio’s
database, which holds it, along with the db volume, which holds what it
encrypts.
3. Deploy it
Section titled “3. Deploy it”kaio-cli stack deploy n8n ./compose.yml --env-file .envkaio-cli stack logs n8n --followThe editor answers on http://localhost:5678, and the first visit creates the
owner account. Webhooks from the outside need a public address: put n8n behind
a reverse proxy and set WEBHOOK_URL to
the address it is reached at, so the URLs n8n shows are the right ones.
4. Upgrade it
Section titled “4. Upgrade it”kaio-cli stack check-update n8n # did stable move?kaio-cli stack update n8n --waitstable moves with each release, and Kaio sees it move without pulling.
--wait answers once every container runs again and Postgres passes its
healthcheck, and puts back the images the stack ran before when it does not get
there within two minutes. To
upgrade on your own schedule instead, pin a release:
kaio-cli stack env set n8n N8N_VERSION=2.1.0kaio-cli stack apply n8n --waitA rollback restores the previous version number for both images at once. It
does not undo the database migrations the newer n8n ran, so read the release
notes before a major version and back up the db volume first.
Kaio, built by Régis Gaidot