Docker Deployment
Relevant source files
The following files were used as context for generating this wiki page:n8n is designed to be deployed using Docker, providing flexibility and scalability. The primary method of deployment is through the official n8n Docker image, which can be run as a standalone container or orchestrated using Docker Compose for more complex setups involving databases and reverse proxies.
This document outlines the architecture of n8n's Docker deployment, including the container entrypoint logic, configuration via environment variables, and various Docker Compose examples for different use cases. The system is configured to ensure proper file permissions and ownership for the n8n user within the container, and it supports various databases and execution modes.
Docker Image
The official n8n Docker image serves as the foundation for all Docker-based deployments. It includes the n8n application and a startup script that prepares the environment before launching the main process.
Container Entrypoint
The docker-entrypoint.sh script is executed when an n8n container starts. Its main responsibilities are to set up the user, create necessary directories with correct permissions, and then execute the n8n application.
The diagram below illustrates the startup sequence of the n8n container.
The script performs the following key actions:
- User Check: It checks if the container is running as the
rootuser. - User and Directory Creation: If running as root, it creates a
nodeuser and group, and also creates the/home/node/.n8ndirectory which is used for storing user data. - Permission Management: It ensures that the
/home/node/.n8ndirectory is owned by thenodeuser. This is crucial for n8n to have the correct permissions to write data like workflows and credentials. - Process Execution: Finally, it uses
tinias an init system to properly manage the n8n process and executes the mainn8ncommand.
Sources: docker/images/n8n/docker-entrypoint.sh
Configuration via Environment Variables
The n8n Docker container is configured primarily through environment variables. These variables control everything from database connections to timezone settings and execution modes.
| Variable | Description | Default Value |
|---|---|---|
DB_TYPE | The type of database to use. For example, postgresdb. | sqlite |
DB_POSTGRESDB_HOST | The hostname of the PostgreSQL database. | localhost |
DB_POSTGRESDB_PORT | The port of the PostgreSQL database. | 5432 |
DB_POSTGRESDB_DATABASE | The name of the database to use. | n8n |
DB_POSTGRESDB_USER | The username for the database connection. | n8n |
DB_POSTGRESDB_PASSWORD | The password for the database connection. | n8n |
N8N_SECURE_COOKIE | Controls the Secure flag on the session cookie. Should be false only for non-HTTPS setups. | true |
GENERIC_TIMEZONE | Sets the timezone for the container, which affects CRON-based workflows. | (Not set) |
N8N_USER_FOLDER | Specifies the folder where n8n user data is stored. | /home/node/.n8n |
N8N_HOST | The hostname to which n8n should bind. | localhost |
N8N_PORT | The port to which n8n should bind. | 5678 |
Sources: docker/images/n8n/README.md, packages/@n8n/benchmark/scripts/n8n-setups/postgres/docker-compose.yml:18-24, docker-compose.yml:18-23
Docker Compose Setups
For production and more advanced development environments, Docker Compose is the recommended way to run n8n. It allows for defining and running multi-container Docker applications, simplifying the management of n8n along with its dependencies like databases and reverse proxies.
Basic Setup with Postgres
A common setup involves running n8n with a PostgreSQL database for persistent storage. This is more robust than the default SQLite database.
The following diagram shows the architecture of a basic n8n deployment with a PostgreSQL database using Docker Compose.
A docker-compose.yml for this setup defines two main services: n8n and postgres.
# packages/@n8n/benchmark/scripts/n8n-setups/postgres/docker-compose.yml:3-29
version: '3.1'
services:
postgres:
image: postgres:11
restart: always
environment:
- POSTGRES_USER=n8n
- POSTGRES_PASSWORD=n8n
- POSTGRES_DB=n8n
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
n8n:
image: n8nio/n8n:latest
restart: always
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=n8n
ports:
- '5678:5678'
links:
- postgres
volumes:
- n8n_data:/home/node/.n8n
volumes:
postgres_data:
n8n_data:
Sources: packages/@n8n/benchmark/scripts/n8n-setups/postgres/docker-compose.yml:3-29
Key aspects of this configuration:
- Services: Defines a
postgresservice using the officialpostgres:11image and ann8nservice usingn8nio/n8n:latest. - Networking: The
n8nservice connects to thepostgresservice using the hostnamepostgres, which is resolved by Docker's internal networking. - Volumes: Named volumes
postgres_dataandn8n_dataare used to persist the database data and n8n's user data, respectively. This ensures data is not lost when containers are restarted or recreated. - Environment: The
n8nservice is configured withDB_TYPE=postgresdband the necessary connection details to communicate with thepostgresservice.
Sources: packages/@n8n/benchmark/scripts/n8n-setups/postgres/docker-compose.yml, docker-compose.yml, packages/nodes-base/nodes/Postgres/Postgres.node.json
Setup with Traefik Reverse Proxy
For production deployments exposed to the internet, it is highly recommended to use a reverse proxy to handle SSL/TLS termination and routing. The repository provides an example using Traefik.
# docker/compose/with-traefik/docker-compose.yml:3-44
version: '3.8'
services:
traefik:
image: 'traefik:v2.10'
restart: always
command:
- '--api.insecure=true'
- '--providers.docker=true'
- '--providers.docker.exposedbydefault=false'
- '--entrypoints.web.address=:80'
- '--entrypoints.websecure.address=:443'
ports:
- '80:80'
- '443:443'
- '8080:8080'
volumes:
- '/var/run/docker.sock:/var/run/docker.sock:ro'
n8n:
image: n8nio/n8n
restart: always
ports:
- '127.0.0.1:5678:5678'
labels:
- 'traefik.enable=true'
- 'traefik.http.routers.n8n.rule=Host(`${SUBDOMAIN}.${DOMAIN_NAME}`)'
- 'traefik.http.routers.n8n.tls=true'
- 'traefik.http.routers.n8n.entrypoints=web,websecure'
- 'traefik.http.middlewares.n8n.headers.SSLProxyHeaders.x-forwarded-proto=https'
environment:
- N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
- N8N_PORT=5678
- N8N_PROTOCOL=https
- NODE_ENV=production
- WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
- GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
volumes:
- '~/.n8n:/home/node/.n8n'
This configuration adds a traefik service that acts as a reverse proxy. The n8n service is no longer directly exposed to the outside world on port 5678. Instead, Traefik routes traffic based on the hostname to the n8n container. Traefik can also automatically manage SSL certificates.
The n8n service is configured with Docker labels that Traefik uses for service discovery and routing configuration.