Skip to content

Docker 安装#

¥Docker Installation

n8n 建议大多数自托管需求都使用 Docker。它提供了一个干净、隔离的环境,避免了操作系统和工具的不兼容性,并简化了数据库和环境管理。

¥n8n recommends using Docker for most self-hosting needs. It provides a clean, isolated environment, avoids operating system and tooling incompatibilities, and makes database and environment management simpler.

你也可以在 Docker 中使用 n8n 和 Docker Compose。你可以在 n8n-hosting 代码仓库 中找到适用于各种架构的 Docker Compose 配置。

¥You can also use n8n in Docker with Docker Compose. You can find Docker Compose configurations for various architectures in the n8n-hosting repository.

Self-hosting knowledge prerequisites

Self-hosting n8n requires technical knowledge, including:

  • Setting up and configuring servers and containers
  • Managing application resources and scaling
  • Securing servers and applications
  • Configuring n8n

n8n recommends self-hosting for expert users. Mistakes can lead to data loss, security issues, and downtime. If you aren't experienced at managing servers, n8n recommends n8n Cloud.

你还可以观看我们的视频指南:

¥You can also follow along with our video guide here:

先决条件#

¥Prerequisites

在继续操作之前,请安装 Docker:

¥Before proceeding, install Docker:

  • Docker Desktop 适用于 Mac、Windows 和 Linux 系统。Docker Desktop 包含 Docker Engine 和 Docker Compose。

¥Docker Desktop is available for Mac, Windows, and Linux. Docker Desktop includes the Docker Engine and Docker Compose.

  • Docker EngineDocker Compose 也提供独立的 Linux 软件包。适用于没有图形环境的 Linux 机器,或当你不需要 Docker Desktop UI 时。

¥Docker Engine and Docker Compose are also available as separate packages for Linux. Use this for Linux machines without a graphical environment or when you don't want the Docker Desktop UI.

Latest and Next versions

n8n releases a new minor version most weeks. The latest version is for production use. next is the most recent release. You should treat next as a beta: it may be unstable. To report issues, use the forum.

Current latest: 1.122.5 Current next: 1.123.2

启动 n8n#

¥Starting n8n

在终端中运行以下命令,并将 <YOUR_TIMEZONE> 占位符替换为 你的时区

¥From your terminal, run the following commands, replacing the <YOUR_TIMEZONE> placeholders with your timezone:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
docker volume create n8n_data

docker run -it --rm \
 --name n8n \
 -p 5678:5678 \
 -e GENERIC_TIMEZONE="<YOUR_TIMEZONE>" \
 -e TZ="<YOUR_TIMEZONE>" \
 -e N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true \
 -e N8N_RUNNERS_ENABLED=true \
 -v n8n_data:/home/node/.n8n \
 docker.n8n.io/n8nio/n8n

此命令创建一个卷来存储持久数据,下载所需的 n8n 镜像,并使用以下设置启动容器:

¥This command creates a volume to store persistent data, downloads the required n8n image, and starts the container with the following settings:

  • 映射并公开主机上的 5678 端口。

¥Maps and exposes port 5678 on the host.

  • 设置容器的时区:

¥Sets the timezone for the container:

¥Enforces secure file permissions for the n8n configuration file.

¥Enables task runners, the recommended way of executing tasks in n8n.

  • n8n_data 卷挂载到 /home/node/.n8n 目录,以便在容器重启后保留数据。

¥Mounts the n8n_data volume to the /home/node/.n8n directory to persist your data across container restarts.

运行后,你可以通过以下方式访问 n8n:http://localhost:5678

¥Once running, you can access n8n by opening: http://localhost:5678

与……一起使用 PostgreSQL#

¥Using with PostgreSQL

默认情况下,n8n 使用 SQLite 保存 credentials、历史执行记录和工作流。n8n 还支持 PostgreSQL,可通过环境变量进行配置,详情如下。

¥By default, n8n uses SQLite to save credentials, past executions, and workflows. n8n also supports PostgreSQL, configurable using environment variables as detailed below.

Persisting the .n8n directory still recommended

使用 PostgreSQL 时,n8n 不需要使用 .n8n 目录来存放 SQLite 数据库文件。但是,该目录仍然包含其他重要数据,例如加密密钥、实例日志和源代码控制功能资源。虽然你可以绕过其中一些要求(例如,通过设置 N8N_ENCRYPTION_KEY 环境变量),但最好还是继续为目录映射持久卷,以避免潜在问题。

¥When using PostgreSQL, n8n doesn't need to use the .n8n directory for the SQLite database file. However, the directory still contains other important data like encryption keys, instance logs, and source control feature assets. While you can work around some of these requirements, (for example, by setting the N8N_ENCRYPTION_KEY environment variable), it's best to continue mapping a persistent volume for the directory to avoid potential issues.

要将 n8n 与 PostgreSQL 一起使用,请执行以下命令,并将占位符(例如 <POSTGRES_USER>)替换为你的实际值:

¥To use n8n with PostgreSQL, execute the following commands, replacing the placeholders (depicted within angled brackets, for example <POSTGRES_USER>) with your actual values:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
docker volume create n8n_data

docker run -it --rm \
 --name n8n \
 -p 5678:5678 \
 -e GENERIC_TIMEZONE="<YOUR_TIMEZONE>" \
 -e TZ="<YOUR_TIMEZONE>" \
 -e N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true \
 -e N8N_RUNNERS_ENABLED=true \
 -e DB_TYPE=postgresdb \
 -e DB_POSTGRESDB_DATABASE=<POSTGRES_DATABASE> \
 -e DB_POSTGRESDB_HOST=<POSTGRES_HOST> \
 -e DB_POSTGRESDB_PORT=<POSTGRES_PORT> \
 -e DB_POSTGRESDB_USER=<POSTGRES_USER> \
 -e DB_POSTGRESDB_SCHEMA=<POSTGRES_SCHEMA> \
 -e DB_POSTGRESDB_PASSWORD=<POSTGRES_PASSWORD> \
 -v n8n_data:/home/node/.n8n \
 docker.n8n.io/n8nio/n8n

你可以在 n8n 托管仓库 中找到完整的 PostgreSQL docker-compose 文件。

¥You can find a complete docker-compose file for PostgreSQL in the n8n hosting repository.

正在更新#

¥Updating

要在 Docker Desktop 中更新 n8n,请导航至“镜像”选项卡,然后从上下文菜单中选择“拉取”以下载最新的 n8n 镜像:

¥To update n8n, in Docker Desktop, navigate to the Images tab and select Pull from the context menu to download the latest n8n image:

Docker Desktop

你还可以使用命令行拉取最新版本或特定版本:

¥You can also use the command line to pull the latest, or a specific version:

1
2
3
4
5
6
7
8
# Pull latest (stable) version
docker pull docker.n8n.io/n8nio/n8n

# Pull specific version
docker pull docker.n8n.io/n8nio/n8n:1.81.0

# Pull next (unstable) version
docker pull docker.n8n.io/n8nio/n8n:next

拉取更新后的镜像后,停止 n8n 容器并重新启动。你还可以使用命令行。将以下命令中的 <container_id> 替换为你在第一个命令中找到的容器 ID:

¥After pulling the updated image, stop your n8n container and start it again. You can also use the command line. Replace <container_id> in the commands below with the container ID you find in the first command:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Find your container ID
docker ps -a

# Stop the container with the `<container_id>`
docker stop <container_id>

# Remove the container with the `<container_id>`
docker rm <container_id>

# Start the container
docker run --name=<container_name> [options] -d docker.n8n.io/n8nio/n8n

正在更新 Docker Compose#

¥Updating Docker Compose

If you run n8n using a Docker Compose file, follow these steps to update n8n:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 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

n8n with tunnel#

Danger

Use this for local development and testing. It isn't safe to use it in production.

To use webhooks for trigger nodes of external services like GitHub, n8n has to be reachable from the web. n8n runs a tunnel service that can redirect requests from n8n's servers to your local n8n instance.

运行以下命令,使用 --tunnel 启动 n8n:

¥Start n8n with --tunnel by running:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
docker volume create n8n_data

docker run -it --rm \
 --name n8n \
 -p 5678:5678 \
 -e GENERIC_TIMEZONE="<YOUR_TIMEZONE>" \
 -e TZ="<YOUR_TIMEZONE>" \
 -e N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true \
 -e N8N_RUNNERS_ENABLED=true \
 -v n8n_data:/home/node/.n8n \
 docker.n8n.io/n8nio/n8n \
 start --tunnel

下一步步骤#

¥Next steps

  • 有关 Docker 设置的更多信息,请参阅 Docker 镜像 的 README 文件。

¥Find more information about Docker setup in the README file for the Docker image.