Docker Compose
以下说明介绍如何在 Linux 服务器上使用 Docker Compose 运行 n8n。
如果已安装 Docker 和 Docker Compose,可以从第 3 步开始。
不同架构的 Docker Compose 配置,可在 n8n-hosting 仓库找到。
自托管的知识要求
自行托管 n8n 需要具备以下技术知识:
- 设置和配置服务器与容器
- 管理应用资源和扩展容量
- 保护服务器和应用的安全
- 配置 n8n
n8n 建议有经验的用户选择自托管。操作失误可能导致数据丢失、安全问题和停机。如果不熟悉服务器管理,n8n 建议使用 n8n Cloud。
Stable 与 Beta 版本
n8n 通常每周发布一个新的次版本。stable 版本用于生产环境;beta 是最近发布的版本;beta 版本可能不稳定。如需报告问题,请使用论坛。
当前 stable:2.45.5
当前 beta:2.46.1
1. 安装 Docker 和 Docker Compose
Docker 和 Docker Compose 的安装方式取决于 Linux 发行版。各组件的具体安装步骤见以下链接:
按说明安装后,输入以下命令验证 Docker 和 Docker Compose 是否可用:
docker --version
docker compose version
2. 可选:非 root 用户访问
可以按需授予无需 sudo 命令即可运行 Docker 的权限。
要为当前登录用户授予访问权限,假定该用户拥有 sudo 权限,请运行:
sudo usermod -aG docker ${USER}
# Register the `docker` group membership with current session without changing your primary group <a href="#register-the-docker-group-membership-with-current-session-without-changing-your-primary-group" id="register-the-docker-group-membership-with-current-session-without-changing-your-primary-group"></a>
exec sg docker newgrp
要为其他用户授予访问权限,请输入以下命令,将 <USER_TO_RUN_DOCKER> 替换为对应用户名:
sudo usermod -aG docker <USER_TO_RUN_DOCKER>
对于该用户已打开的会话,需要运行 exec sg docker newgrp,才能使用新的用户组权限。
输入以下命令,确认当前会话识别 docker 用户组:
groups
3. 设置 DNS
要在互联网或网络中托管 n8n,请创建指向服务器的专用子域名。
添加 A 记录,将子域名路由到对应地址:
| 记录类型 | 名称 | 目标 |
|---|---|---|
| A | n8n,或你希望使用的子域名 | <your_server_IP_address> |
4. 创建 .env 文件
创建项目目录,用于保 存 n8n 环境配置和 Docker Compose 文件,并进入目录:
mkdir n8n-compose
cd n8n-compose
在 n8n-compose 目录中创建 .env 文件,定制 n8n 实例详情。请更改为你自己的信息:
# DOMAIN_NAME and SUBDOMAIN together determine where n8n will be reachable from <a href="#domainname-and-subdomain-together-determine-where-n8n-will-be-reachable-from" id="domainname-and-subdomain-together-determine-where-n8n-will-be-reachable-from"></a>
# The top level domain to serve from <a href="#the-top-level-domain-to-serve-from" id="the-top-level-domain-to-serve-from"></a>
DOMAIN_NAME=example.com
# The subdomain to serve from <a href="#the-subdomain-to-serve-from" id="the-subdomain-to-serve-from"></a>
SUBDOMAIN=n8n
# The above example serve n8n at: https://n8n.example.com <a href="#the-above-example-serve-n8n-at-httpsn8nexamplecom" id="the-above-example-serve-n8n-at-httpsn8nexamplecom"></a>
# Optional timezone to set which gets used by Cron and other scheduling nodes <a href="#optional-timezone-to-set-which-gets-used-by-cron-and-other-scheduling-nodes" id="optional-timezone-to-set-which-gets-used-by-cron-and-other-scheduling-nodes"></a>
# New York is the default value if not set <a href="#new-york-is-the-default-value-if-not-set" id="new-york-is-the-default-value-if-not-set"></a>
GENERIC_TIMEZONE=Europe/Berlin
# The email address to use for the TLS/SSL certificate creation <a href="#the-email-address-to-use-for-the-tlsssl-certificate-creation" id="the-email-address-to-use-for-the-tlsssl-certificate-creation"></a>
SSL_EMAIL=user@example.com