Self-hosting n8n with Docker Compose
Updated 2026-10-21 · 6 min read
- One docker run command is enough to try n8n. For production, use Compose with Postgres.
- Code and Python nodes need the n8nio/runners sidecar, on the same version as n8n.
- Back up the database and the encryption key. Without the key, credentials are unreadable.
Self-hosting n8n with Docker removes the limits of n8n Cloud. You can use npm packages in Code nodes and Python with libraries. You don't pay per execution, and your data stays on your own server. In exchange, you run the database, the runners, the backups and the upgrades yourself.
This guide starts with a one-line test container. It ends with a production Compose file: Postgres plus the task runner sidecar that n8n 2.x needs for Code nodes and Python. A task runner is the separate process n8n uses to run Code node code. A sidecar is a second container that runs next to n8n.
n8n itself says self-hosting is recommended for expert users (Docker docs). Have you never run a database in production? Then read n8n Cloud vs self-hosted first.
What to do now
- Try n8n in one command. Run the test container from Step 0 and click through your first workflow.
- Set up the Compose file. Copy
.envandcompose.ymlbelow, change everychange-mevalue and the host name. - Put a proxy with TLS in front. With Caddy, the config below is all you need.
- Schedule backups. Dump the database daily and keep a copy of the encryption key off the server.
- Check your Pipedream code. Count the steps that import npm packages before you build a custom runners image. The converter report lists them.
Step 0: try it in one command
docker volume create n8n_data
docker run -it --rm --name n8n -p 5678:5678 \
-e GENERIC_TIMEZONE="Europe/Berlin" -e TZ="Europe/Berlin" \
-e N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true \
-v n8n_data:/home/node/.n8n \
n8nio/n8n
Open http://localhost:5678 and create the owner account. All data lives in the n8n_data volume. That includes the SQLite database and the encryption key. This is good for a test, not for production. There is no Postgres, no external runners and no TLS.
What a production setup needs
| Piece | Why |
|---|---|
| Postgres | n8n recommends it over SQLite for real workloads |
n8nio/runners sidecar |
External-mode task runners: real isolation for Code nodes, and required for Python |
Fixed N8N_ENCRYPTION_KEY |
Credentials stay readable when the container is recreated |
| Timezone | The self-hosted default is America/New_York |
| Reverse proxy with TLS | HTTPS, and correct public webhook URLs |
| Backups | A database dump plus the key |
The Compose file
Create a folder with two files: .env and compose.yml.
.env (keep it out of version control):
N8N_VERSION=2.44.4
POSTGRES_USER=n8n
POSTGRES_PASSWORD=change-me
POSTGRES_DB=n8n
N8N_ENCRYPTION_KEY=change-me-long-random-string
N8N_RUNNERS_AUTH_TOKEN=change-me-another-random-string
Pin N8N_VERSION to a current stable release. The runners image must use the same version as n8n (task runners).
compose.yml:
services:
postgres:
image: postgres:18
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data
volumes:
- db-storage:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 10
n8n:
image: n8nio/n8n:${N8N_VERSION}
restart: always
ports:
- "127.0.0.1:5678:5678"
environment:
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_PORT: "5432"
DB_POSTGRESDB_DATABASE: ${POSTGRES_DB}
DB_POSTGRESDB_USER: ${POSTGRES_USER}
DB_POSTGRESDB_PASSWORD: ${POSTGRES_PASSWORD}
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
GENERIC_TIMEZONE: Europe/Berlin
TZ: Europe/Berlin
N8N_HOST: n8n.example.com
N8N_PROTOCOL: https
N8N_WEBHOOK_URL: https://n8n.example.com/
N8N_PROXY_HOPS: "1"
N8N_RUNNERS_MODE: external
N8N_RUNNERS_BROKER_LISTEN_ADDRESS: 0.0.0.0
N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
volumes:
- n8n-data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
task-runners:
image: n8nio/runners:${N8N_VERSION}
restart: always
environment:
N8N_RUNNERS_TASK_BROKER_URI: http://n8n:5679
N8N_RUNNERS_AUTH_TOKEN: ${N8N_RUNNERS_AUTH_TOKEN}
depends_on:
- n8n
volumes:
db-storage:
n8n-data:
Start it:
docker compose up -d
docker compose logs -f n8n
Why the file looks like this:
- Postgres 18 with
PGDATAset. n8n's Compose guide does this so the data path matches earlier Postgres versions. - Port bound to 127.0.0.1. Only the reverse proxy on the same host can reach n8n directly.
N8N_WEBHOOK_URL. Use it on n8n 2.30.0 and later. Older versions useWEBHOOK_URL. Without it, the editor shows webhook URLs withlocalhost:5678, and senders can't reach them.N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0. The broker is the part of n8n that hands Code node tasks to the runners. By default it listens only on localhost. The sidecar connects from another container, so it must listen on all addresses.- No
N8N_RUNNERS_ENABLED. It is deprecated from 2.0, where task runners are on by default.
The n8n Compose guide also includes a sandbox stack for the n8n Assistant (AI code execution). It is optional and needs more resources: at least 4 GB RAM and 2 vCPUs, per that guide. The file above leaves it out.
Reverse proxy
Put Caddy, Traefik or nginx in front of n8n for TLS. With Caddy, this is the whole config:
n8n.example.com {
reverse_proxy 127.0.0.1:5678
}
Set N8N_PROXY_HOPS to the number of proxies in front of n8n. Otherwise IP allowlists on webhooks see the proxy's address, not the caller's.
npm packages and Python libraries
Packages for Code nodes go into the runners image, not into the main n8n container. You also allow-list them in the runners' launcher config:
FROM n8nio/runners:2.44.4
USER root
RUN cd /opt/runners/task-runner-javascript && pnpm add lodash dayjs
RUN cd /opt/runners/task-runner-python && uv pip install pandas
COPY n8n-task-runners.json /etc/n8n-task-runners.json
USER runner
In n8n-task-runners.json, each runner has an env-overrides block. Set NODE_FUNCTION_ALLOW_EXTERNAL there for JavaScript and N8N_RUNNERS_EXTERNAL_ALLOW for Python. Build the image and use it in the Compose file instead of n8nio/runners. The full config, step by step: npm packages in n8n. For Python, see Python Code node.
Backups
- Database. Run
docker compose exec postgres pg_dump -U n8n n8n > n8n-$(date +%F).sqldaily and copy the file off the server. - Encryption key. It is in your
.envand in then8n-datavolume. Without it, restored credentials are unreadable. - Workflows as files (optional). Run
n8n export:workflow --backup --output=backups/inside the container. You get JSON files you can keep in Git.
Test a restore once on a second machine before you rely on it.
Upgrades
- Read the release notes for breaking changes. Version 2.0, for example, changed Code node runners, Python, env access and publishing.
- Back up the database.
- Change
N8N_VERSIONin.env. Both stock images follow it. Did you build a custom runners image? Rebuild it from the same version first. - Run
docker compose pull && docker compose up -d. n8n runs its database migrations on start.
Free vs licensed features
The self-hosted Community edition is free and includes almost all of n8n. Among other things, it lacks:
- Variables, environments and external secrets
- log streaming and SSO
- credential sharing, projects and Git source control
If you register your email, you also get folders, debug in editor and custom execution data (Community edition features). Business and Enterprise licenses unlock the rest.
When to scale
One n8n container handles a lot. Do executions queue up, or does the editor get slow during heavy runs? Then move to queue mode: Redis plus worker containers, each with its own runners sidecar.
Self-hosting as a Pipedream replacement
Teams leaving Pipedream often self-host. The reason: Pipedream code steps could import any npm package, but n8n Cloud allows only crypto and moment in the Code node.
Before you choose, count how many of your steps really import packages. Our converter report lists every import per workflow. The Code node compatibility check does the same for one step. The report and 1 workflow are free. Full export: $3 per workflow, $1 each after 50, $19 minimum, one-off. Then follow migrate Pipedream to n8n with the instance you just set up.
Not affiliated with Pipedream or n8n.
FAQ
What is the quickest way to run n8n in Docker?
Run docker volume create n8n_data, then docker run -it --rm --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n n8nio/n8n. Open http://localhost:5678. For anything long-lived, use Docker Compose with Postgres.
Do I need the n8nio/runners image?
Yes, for Python Code nodes and for proper isolation of JavaScript Code nodes. External-mode task runners run in an n8nio/runners sidecar. Its version must match n8nio/n8n. Since 2.0 the main image doesn't include the external-mode runner.
Should n8n use SQLite or Postgres?
SQLite is the default and fine for testing. n8n's Compose guide recommends Postgres for production instances with more than a handful of users or workflows running around the clock.
How much RAM does self-hosted n8n need?
n8n's own Docker page doesn't state minimum hardware for n8n alone. Its full Compose guide includes the n8n Assistant sandbox stack and asks for at least 4 GB RAM and 2 vCPUs.
What must I back up?
The database (or the n8n_data volume if you use SQLite) and the encryption key. Credentials can't be decrypted without the key.
Sources (6)
- https://docs.n8n.io/deploy/host-n8n/install-options/install-with-docker
- https://docs.n8n.io/deploy/host-n8n/install-options/install-using-docker-compose
- https://docs.n8n.io/deploy/host-n8n/configure-n8n/set-up-task-runners
- https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/deployment
- https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/endpoints
- https://docs.n8n.io/deploy/host-n8n/community-edition-features