为什么折腾 n8n
最近需要把一些重复性通知和数据处理自动化,选型时看中了 n8n——它开源、可自托管,拖拽节点就能搭流程,还支持代码节点。部署上比想象中简单,但要让界面中文舒服、数据落库、顺带接上本地 Ollama 模型,还是踩了几个小坑。这里记下在 Ubuntu 24.04 上用 Docker Compose 部署的配置,方便以后复用。
最小起步:直接跑起来
先试试水,一个最简的 docker-compose.yml 就能把 n8n 和 PostgreSQL 拉起来。创建项目目录和 .env:
mkdir n8n && cd n8n
.env 内容,把密码替换成自己的:
# .env
DB_PASSWORD=superpass123
ENCRYPTION_KEY=superpass123
然后写 compose 文件:
services:
n8n:
image: n8nio/n8n:latest
container_name: n8n-production
restart: unless-stopped
ports:
- "5678:5678"
environment:
- TZ=Asia/Shanghai
- GENERIC_TIMEZONE=Asia/Shanghai
- N8N_DEFAULT_LOCALE=zh-CN
- N8N_LANG=zh-CN
- LC_ALL=zh_CN.UTF-8
- LANG=zh_CN.UTF-8
- LANGUAGE=zh_CN:zh
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_DATABASE=n8ndb
- DB_POSTGRESDB_USER=n8nuser
- DB_POSTGRESDB_PASSWORD=${DB_PASSWORD}
volumes:
- n8n_data:/home/node/.n8n
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:5678/healthz"]
interval: 30s
timeout: 10s
retries: 3
postgres:
image: postgres:16-alpine
container_name: n8n-postgres
restart: unless-stopped
environment:
- POSTGRES_USER=n8nuser
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=n8ndb
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U n8nuser -d n8ndb"]
interval: 10s
timeout: 5s
retries: 5
volumes:
n8n_data:
postgres_data:
启动:
docker compose up -d
访问 http://localhost:5678 就能看到中文界面了。不过这个镜像虽然是中文 locale,但 UI 翻译并不完整,一些细节还是英文。
换成纯血中文界面
社区有汉化项目 other-blowsnow/n8n-i18n-chinese,把汉化后的 UI 文件挂载进去,体验好很多。用特定版本(比如 2.3.6)更稳妥,因为汉化分支是跟着 n8n 版本走的。
先拉取汉化资源:
git clone https://github.com/other-blowsnow/n8n-i18n-chinese
cd n8n-i18n-chinese
git checkout [email protected]
在当前目录下会得到一个 editor-ui-dist 文件夹,后面挂载时要用。创建数据目录并修复权限,避免容器内 node 用户写不进去:
mkdir -p n8n-data
sudo chown -R 1000:1000 ./n8n-data
sudo chmod -R 755 ./n8n-data
然后写新的 compose 文件,注意换成 n8nio/n8n:2.3.6,挂载刚才的 editor-ui-dist,并用 PostgreSQL 15(跟汉化分支适配时的版本一致):
services:
n8ntest:
image: n8nio/n8n:2.3.6
container_name: n8ntest
ports:
- "15678:5678"
environment:
- N8N_DEFAULT_LOCALE=zh-CN
- N8N_SECURE_COOKIE=false
- N8N_RUNNERS_ENABLED=true
- N8N_ENCRYPTION_KEY=superpass123
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n_user
- DB_POSTGRESDB_PASSWORD=superpass123
volumes:
- ./n8n-data:/home/node/.n8n
- ./editor-ui-dist:/usr/local/lib/node_modules/n8n/node_modules/n8n-editor-ui/dist
depends_on:
- postgres
stdin_open: true
tty: true
restart: unless-stopped
postgres:
image: postgres:15
container_name: n8n-postgres
environment:
- POSTGRES_DB=n8n
- POSTGRES_USER=n8n_user
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:
这次映射的是 15678 端口,启动后访问 http://localhost:15678。注意 .env 文件里的 DB_PASSWORD 要对应上。
如果你需要切换分支或回退,熟悉的 git 命令都在:
git status # 了解当前状态
git switch main # 切换回主分支
git stash # 暂存修改
git commit # 提交更改
git tag # 查看标签
顺便接到本地 Ollama
我本机跑着 Ollama,想从 n8n 里直接调模型。n8n 本身有 Ollama 节点,只要在 compose 里加一行 extra_hosts,让容器能访问宿主机:
extra_hosts:
- "host.docker.internal:host-gateway"
加上后,完整配置就变成这样(仅 n8ntest 服务部分):
services:
n8ntest:
image: n8nio/n8n:2.3.6
container_name: n8ntest
ports:
- "15678:5678"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
- N8N_DEFAULT_LOCALE=zh-CN
- N8N_SECURE_COOKIE=false
- N8N_RUNNERS_ENABLED=true
- N8N_ENCRYPTION_KEY=superpass123
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n_user
- DB_POSTGRESDB_PASSWORD=superpass123
volumes:
- ./n8n-data:/home/node/.n8n
- ./editor-ui-dist:/usr/local/lib/node_modules/n8n/node_modules/n8n-editor-ui/dist
depends_on:
- postgres
stdin_open: true
tty: true
restart: unless-stopped
postgres 服务保持不变。然后重新 docker compose up -d,在 n8n 工作流里添加 Ollama 节点,填入 http://host.docker.internal:11434 作为 URL 就能用了。
简单粗暴,一趟跑通。
注意:以上配置里有些地方直接写了明文密码,生产环境建议用 secrets 管理。

