179 lines
3.9 KiB
Markdown
179 lines
3.9 KiB
Markdown
|
||
#n8n #docker #workflow
|
||
|
||
|
||
|
||
## n8n Docker install
|
||
### n8n Docker Compose file
|
||
``` bash
|
||
cd /home/shenwei/Docker/n8n
|
||
```
|
||
|
||
create **docker-compose.yml** file
|
||
``` yaml
|
||
|
||
version: '3.8'
|
||
services:
|
||
n8n:
|
||
build: .
|
||
image: docker.n8n.io/n8nio/n8n
|
||
container_name: n8n
|
||
ports:
|
||
- "5678:5678" # 只绑定到本地,通过 Caddy 访问
|
||
volumes:
|
||
- n8n_data:/home/node/.n8n
|
||
environment:
|
||
- N8N_PROTOCOL=https
|
||
- N8N_HOST=n8n.ishenwei.online
|
||
- WEBHOOK_URL=https://n8n.ishenwei.online/
|
||
- N8N_TRUST_PROXY=true
|
||
- N8N_SECURE_COOKIE=true # 建议设为 true,因为使用 HTTPS
|
||
- N8N_PROXY_HOPS=1
|
||
- ALL_PROXY=socks5://172.21.0.1:10808 #配置容器内网络代理
|
||
restart: unless-stopped
|
||
|
||
volumes:
|
||
n8n_data:
|
||
|
||
networks:
|
||
n8n_default:
|
||
external: true
|
||
|
||
```
|
||
|
||
Dockerfile
|
||
```
|
||
FROM n8nio/n8n:latest
|
||
USER root
|
||
|
||
# 安装 curl 和 wget
|
||
RUN apk update && apk add --no-cache curl wget
|
||
|
||
USER node
|
||
```
|
||
|
||
### Updating Docker Compose
|
||
[Doc](https://docs.n8n.io/hosting/installation/docker/#updating-docker-compose "Permanent link")
|
||
|
||
If you run n8n using a Docker Compose file, follow these steps to update n8n:
|
||
|
||
``` bash
|
||
# Navigate to the directory containing your docker compose file
|
||
cd </path/to/your/compose/file/directory>
|
||
|
||
# Pull latest version
|
||
docker compose pull
|
||
|
||
# Stop and remove older version
|
||
docker compose down
|
||
|
||
# Start the container
|
||
docker compose up -d
|
||
```
|
||
|
||
### Config n8n network proxy
|
||
|
||
#### 1️⃣ 前提条件
|
||
|
||
1. V2Ray/Tuic 已安装在宿主机并正常运行。
|
||
2. V2Ray/Tuic 配置中 **本地监听地址改为 `0.0.0.0`**,端口假设为 `10808`:
|
||
在V2rayN GUI里需要打开如下配置:
|
||
![[IMG-20251230094029556.png]]
|
||
|
||
3. Docker 网络 `n8n_default` 已存在(由 docker-compose 自动创建即可)
|
||
4. 宿主机防火墙允许 Docker 网桥访问代理端口:
|
||
```
|
||
sudo ufw allow from 172.18.0.0/16 to any port 10808
|
||
```
|
||
#### 2️⃣ Dockerfile(扩展官方 n8n 镜像,安装 curl/wget)
|
||
|
||
创建 `Dockerfile`:
|
||
``` bash
|
||
FROM n8nio/n8n:latest
|
||
|
||
USER root
|
||
|
||
# 安装 curl 和 wget
|
||
RUN apk update && apk add --no-cache curl wget
|
||
|
||
USER node
|
||
```
|
||
- 保持 n8n 默认用户 `node`,安全性高。
|
||
- 容器内可以直接使用 `curl`、`wget` 测试代理。
|
||
|
||
---
|
||
#### 3️⃣ docker-compose.yml 示例
|
||
``` yaml
|
||
|
||
version: '3.8'
|
||
services:
|
||
n8n:
|
||
build: .
|
||
image: docker.n8n.io/n8nio/n8n
|
||
container_name: n8n
|
||
ports:
|
||
- "5678:5678" # 只绑定到本地,通过 Caddy 访问
|
||
volumes:
|
||
- n8n_data:/home/node/.n8n
|
||
environment:
|
||
- N8N_PROTOCOL=https
|
||
- N8N_HOST=n8n.ishenwei.online
|
||
- WEBHOOK_URL=https://n8n.ishenwei.online/
|
||
- N8N_TRUST_PROXY=true
|
||
- N8N_SECURE_COOKIE=true # 建议设为 true,因为使用 HTTPS
|
||
- N8N_PROXY_HOPS=1
|
||
- ALL_PROXY=socks5://172.21.0.1:10808 #配置容器内网络代理
|
||
restart: unless-stopped
|
||
|
||
volumes:
|
||
n8n_data:
|
||
|
||
networks:
|
||
n8n_default:
|
||
external: true
|
||
```
|
||
|
||
说明:
|
||
|
||
- `ALL_PROXY` 指向宿主机 Docker 网桥 IP + Tuic SOCKS5 端口
|
||
- 容器内 HTTP/HTTPS 流量和 n8n 请求都会走 SOCKS5
|
||
- 端口 5678 映射宿主机,便于访问 n8n UI
|
||
|
||
> [!注意]
|
||
注意:`172.21.0.1` 需替换为以下命令输出的网桥 IP(Gateway)。
|
||
``` bash
|
||
|
||
docker network inspect n8n_default
|
||
|
||
```
|
||
|
||
---
|
||
|
||
#### 4️⃣ 在容器内测试科学上网
|
||
|
||
进入容器:
|
||
```
|
||
docker exec -it n8n /bin/sh
|
||
```
|
||
测试:
|
||
```
|
||
# 测试 IP
|
||
curl --socks5 172.18.0.1:10808 https://ifconfig.me
|
||
|
||
# 或者使用全局代理环境变量
|
||
curl https://ifconfig.me
|
||
wget -qO- https://ifconfig.me
|
||
```
|
||
|
||
如果返回国外 IP,说明代理生效。
|
||
|
||
---
|
||
#### 5️⃣ 可选优化
|
||
|
||
- **Dockerfile 内设置环境变量**:可直接在镜像内定义 `ALL_PROXY`,启动容器无需手动设置。
|
||
- **安全防护**:宿主机防火墙限制仅 Docker 网桥访问 10808,避免局域网被访问。
|
||
- **升级 n8n**:定期 rebuild 镜像即可。
|
||
|
||
## Reference
|
||
|
||
[[n8n configure telegram trigger|n8n configure telegram trigger]] |