Docker-Compose
如果您已经安装了Docker和Docker-Compose,那么您可以直接从步骤3 开始。
你可以在n8n-hosting仓库 中找到适用于各种架构的Docker Compose配置。
自托管知识先决条件
自托管n8n需要具备以下技术知识:
设置和配置服务器及容器
管理应用程序资源和扩展
保护服务器和应用程序安全
配置n8n
n8n 建议专家用户自行托管。操作失误可能导致数据丢失、安全问题和服务中断。如果您没有服务器管理经验,n8n 推荐使用 n8n Cloud 。
最新版本和下一版本
n8n 每周都会发布一个新的次要版本。latest 版本适用于生产环境。next 是最新发布的版本。您应该将 next 视为测试版:它可能不稳定。如需报告问题,请使用 论坛 。
当前 latest: 1.92.2
当前 next: 1.93.0
1. 安装Docker和Docker Compose
安装Docker和Docker Compose的方法因您使用的Linux发行版而异。您可以在Docker 和Docker Compose 的安装文档中找到详细说明。以下示例适用于Ubuntu系统:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 # Remove incompatible or out of date Docker implementations if they exist
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg ; done
# Install prereq packages
sudo apt-get update
sudo apt-get install ca-certificates curl
# Download the repo signing key
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Configure the repository
echo "deb [arch= $( dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $( . /etc/os-release && echo " ${ UBUNTU_CODENAME :- $VERSION_CODENAME } " ) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update and install Docker and Docker Compose
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
通过输入以下命令验证Docker和Docker Compose是否可用:
docker --version
docker compose version
2. 可选:非root用户访问权限
你可以选择授予权限,无需使用sudo命令即可运行Docker。
要授予当前登录用户访问权限(假设他们拥有sudo权限),请运行:
sudo usermod -aG docker ${ USER }
# Register the `docker` group memebership with current session without changing your primary group
exec sg docker newgrp
要授予其他用户访问权限,请输入以下命令,将替换为相应的用户名:
sudo usermod -aG docker <USER_TO_RUN_DOCKER>
您需要在该用户的任何现有会话中运行exec sg docker newgrp命令,以便其访问新的组权限。
你可以通过输入以下命令来验证当前会话是否识别docker用户组:
3. DNS设置
要在线上或网络中托管n8n,请创建一个指向您服务器的专用子域名。
添加一条A记录以相应路由子域名:
类型 : A
名称 : n8n (或所需的子域名)
IP地址 : (你的服务器IP地址)
4. 创建一个 .env 文件
创建一个项目目录来存储您的n8n环境配置和Docker Compose文件,并进入该目录:
mkdir n8n-compose
cd n8n-compose
在n8n-compose目录内,创建一个.env文件来自定义您的n8n实例详情。请根据您的实际情况修改以下内容:
.env 文件 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 # DOMAIN_NAME and SUBDOMAIN together determine where n8n will be reachable from
# The top level domain to serve from
DOMAIN_NAME = example.com
# The subdomain to serve from
SUBDOMAIN = n8n
# The above example serve n8n at: https://n8n.example.com
# Optional timezone to set which gets used by Cron and other scheduling nodes
# New York is the default value if not set
GENERIC_TIMEZONE = Europe/Berlin
# The email address to use for the TLS/SSL certificate creation
SSL_EMAIL = user@example.com
5. 创建本地文件目录
在您的项目目录中,创建一个名为local-files的目录,用于在n8n实例和主机系统之间共享文件(例如,使用Read/Write Files from Disk node ):
下面的Docker Compose文件可以自动创建这个目录,但手动操作能确保它以正确的所有权和权限被创建。
6. 创建Docker Compose文件
创建一个docker-compose.yml文件。将以下内容粘贴到文件中:
docker-compose.yml 文件 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 services :
traefik :
image : "traefik"
restart : always
command :
- "--api=true"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entrypoints.web.http.redirections.entryPoint.to=websecure"
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
- "--certificatesresolvers.mytlschallenge.acme.email=${SSL_EMAIL}"
- "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"
ports :
- "80:80"
- "443:443"
volumes :
- traefik_data:/letsencrypt
- /var/run/docker.sock:/var/run/docker.sock:ro
n8n :
image : docker.n8n.io/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.routers.n8n.tls.certresolver=mytlschallenge
- traefik.http.middlewares.n8n.headers.SSLRedirect=true
- traefik.http.middlewares.n8n.headers.STSSeconds=315360000
- traefik.http.middlewares.n8n.headers.browserXSSFilter=true
- traefik.http.middlewares.n8n.headers.contentTypeNosniff=true
- traefik.http.middlewares.n8n.headers.forceSTSHeader=true
- traefik.http.middlewares.n8n.headers.SSLHost=${DOMAIN_NAME}
- traefik.http.middlewares.n8n.headers.STSIncludeSubdomains=true
- traefik.http.middlewares.n8n.headers.STSPreload=true
- traefik.http.routers.n8n.middlewares=n8n@docker
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_data:/home/node/.n8n
- ./local-files:/files
volumes :
n8n_data :
traefik_data :
上述Docker Compose文件配置了两个容器:一个用于n8n,另一个运行traefik (一个用于管理TLS/SSL证书和处理路由的应用代理)。
它还会创建并挂载两个Docker卷 ,并挂载您之前创建的local-files目录:
名称
类型
容器挂载
描述
n8n_data
Volume
/home/node/.n8n
Where n8n saves its SQLite database file and encryption key.
traefik_data
Volume
/letsencrypt
Where traefik saves the TLS/SSL certificate data.
./local-files
Bind
/files
A local directory shared between the n8n instance and host. In n8n, use the /files path to read from and write to this directory.
7. 启动Docker Compose
你现在可以通过输入以下命令启动n8n:
sudo docker compose up -d
要停止容器,请输入:
8. 完成
现在您可以通过在.env文件配置中定义的子域名+域名组合来访问n8n。上面的示例将生成https://n8n.example.com。
n8n仅可通过安全的HTTPS访问,不支持明文HTTP。
下一步