aigenai commited on
Commit
ee6ea45
1 Parent(s): 75333eb
Files changed (4) hide show
  1. Dockerfile +111 -0
  2. README.md +0 -10
  3. import-db.sh +42 -0
  4. run.sh +27 -0
Dockerfile ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 基础构建阶段:用于构建和安装依赖
2
+ FROM python:3.10-slim AS builder
3
+
4
+ # 设置构建时变量
5
+ ARG requirements
6
+ ARG NODEJS_VER=20
7
+ ARG PACKAGES=n8n
8
+ ARG WORKDIR=/app
9
+ ARG DUMP_URL
10
+ ARG DUMP_PASSWORD
11
+
12
+ # 安装 Node.js、n8n 以及必要的系统工具
13
+ RUN apt-get update && apt-get install -y curl gnupg build-essential && \
14
+ curl -fsSL https://deb.nodesource.com/setup_${NODEJS_VER}.x | bash - && \
15
+ apt-get install -y nodejs && \
16
+ npm install -g ${PACKAGES} && \
17
+ apt-get clean && rm -rf /var/lib/apt/lists/*
18
+
19
+ # 设置 Python 虚拟环境
20
+ ENV VIRTUAL_ENV=/opt/venv
21
+ RUN python3 -m venv $VIRTUAL_ENV && \
22
+ $VIRTUAL_ENV/bin/pip install --upgrade pip && \
23
+ $VIRTUAL_ENV/bin/pip install ${requirements:-requests}
24
+
25
+ # 主运行阶段:基于较小的 PostgreSQL 镜像
26
+ FROM postgres:latest
27
+
28
+ # 设置环境变量
29
+ ARG POSTGRES_USER=n8n
30
+ ARG POSTGRES_PASSWORD=n8n
31
+ ARG POSTGRES_DB=n8n
32
+ ARG WEBHOOK_URL=https://aigenai-db.hf.space/
33
+ ARG WORKDIR=/app
34
+ ARG DB_IMPORT=no # 新增 ARG 用于控制数据库导入
35
+
36
+ # 设置 n8n 环境变量
37
+ ENV N8N_HOST=0.0.0.0 \
38
+ N8N_PORT=7860 \
39
+ N8N_PROTOCOL=https \
40
+ WEBHOOK_URL=${WEBHOOK_URL} \
41
+ GENERIC_TIMEZONE=Asia/Shanghai \
42
+ N8N_METRICS=true \
43
+ QUEUE_HEALTH_CHECK_ACTIVE=true \
44
+ N8N_PAYLOAD_SIZE_MAX=256
45
+
46
+ # 设置数据库连接相关的环境变量
47
+ ENV DB_TYPE=postgresdb \
48
+ DB_POSTGRESDB_HOST=localhost \
49
+ DB_POSTGRESDB_PORT=5432 \
50
+ DB_POSTGRESDB_USER=${POSTGRES_USER} \
51
+ DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD} \
52
+ DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
53
+
54
+ # 设置 Python 环境变量
55
+ ENV VIRTUAL_ENV=/opt/venv \
56
+ PATH="$VIRTUAL_ENV/bin:$PATH"
57
+
58
+ # 复制构建阶段的 n8n 和 Python 运行环境
59
+ COPY --from=builder /usr/local/bin/node /usr/local/bin/node
60
+ COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
61
+ COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV
62
+
63
+ # 安装必要的软件包
64
+ RUN apt-get update && apt-get install -y \
65
+ curl unzip gnupg build-essential sudo vim git procps lsof net-tools \
66
+ ca-certificates openssl tzdata python3-venv gosu \
67
+ htop jq wget && \
68
+ ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
69
+ dpkg-reconfigure --frontend noninteractive tzdata
70
+
71
+ # 设置工作目录并复制启动脚本
72
+ WORKDIR ${WORKDIR}
73
+ COPY run.sh ${WORKDIR}/run.sh
74
+ RUN chmod +x ${WORKDIR}/run.sh
75
+
76
+ # 更改现有的 postgres 用户 UID 和 GID 为 1000
77
+ USER root
78
+ RUN usermod -u 1000 postgres && groupmod -g 1000 postgres && \
79
+ chown -R postgres:postgres /var/lib/postgresql && \
80
+ chown -R postgres:postgres /var/run/postgresql
81
+
82
+ # 切换到 postgres 用户,初始化数据库并创建角色
83
+ USER postgres
84
+ RUN initdb -D /var/lib/postgresql/data && \
85
+ pg_ctl start -D /var/lib/postgresql/data && \
86
+ psql --command "CREATE ROLE $POSTGRES_USER WITH LOGIN SUPERUSER PASSWORD '$POSTGRES_PASSWORD';" && \
87
+ createdb -O $POSTGRES_USER $POSTGRES_DB && \
88
+ pg_ctl stop -D /var/lib/postgresql/data
89
+
90
+ # 复制数据库导入脚本和数据文件
91
+ COPY dump.sql /docker-entrypoint-initdb.d/
92
+
93
+ # 添加条件导入的 shell 脚本
94
+ COPY import-db.sh /docker-entrypoint-initdb.d/
95
+ RUN chmod +x /docker-entrypoint-initdb.d/import-db.sh
96
+
97
+ # 脚本 import-db.sh 的内容:
98
+ # #!/bin/bash
99
+ # if [ "$DB_IMPORT" = "yes" ]; then
100
+ # echo "Importing database dump..."
101
+ # psql -U $POSTGRES_USER -d $POSTGRES_DB -f /docker-entrypoint-initdb.d/dump.sql
102
+ # else
103
+ # echo "Skipping database import."
104
+ # fi
105
+
106
+ # 健康检查配置(可选)
107
+ HEALTHCHECK --interval=120s --timeout=10s --start-period=10s --retries=3 \
108
+ CMD curl -f http://localhost:7860/HEALTHZ || exit 1
109
+
110
+ # 启动容器时执行run.sh脚本
111
+ CMD ["./run.sh"]
README.md DELETED
@@ -1,10 +0,0 @@
1
- ---
2
- title: N8n Db
3
- emoji: 🐠
4
- colorFrom: gray
5
- colorTo: red
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
import-db.sh ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 检查是否需要导入数据库
4
+ if [ "$DB_IMPORT" = "yes" ]; then
5
+ echo "Starting database import..."
6
+
7
+ # 读取下载 URL 和解压密码(如果有)
8
+ DUMP_URL=${DUMP_URL:-""} # 数据库 dump 文件的下载 URL
9
+ DUMP_PASSWORD=${DUMP_PASSWORD:-""} # 如果 dump 文件有密码保护
10
+
11
+ # 检查 URL 是否为空
12
+ if [ -z "$DUMP_URL" ]; then
13
+ echo "DUMP_URL is not set. Skipping database import."
14
+ # 不退出,仅跳过导入步骤
15
+ exit 0
16
+ fi
17
+
18
+ # 下载数据库 dump 文件
19
+ echo "Downloading dump file from $DUMP_URL..."
20
+ wget -O /tmp/dump.sql "$DUMP_URL" || { echo "Failed to download dump file."; exit 1; }
21
+
22
+ # 检查文件是否有密码保护,并使用 pg_restore 或 psql 进行导入
23
+ if [ -n "$DUMP_PASSWORD" ]; then
24
+ echo "Dump file is protected with a password."
25
+
26
+ # 使用 pg_restore 导入
27
+ PGPASSWORD="$DUMP_PASSWORD" pg_restore -U $DB_POSTGRESDB_USER -d $DB_POSTGRESDB_DATABASE /tmp/dump.sql || {
28
+ echo "Failed to import dump file with password."; exit 1;
29
+ }
30
+ else
31
+ echo "Dump file is not password protected."
32
+
33
+ # 使用 psql 直接导入
34
+ psql -U $DB_POSTGRESDB_USER -d $DB_POSTGRESDB_DATABASE -f /tmp/dump.sql || {
35
+ echo "Failed to import dump file."; exit 1;
36
+ }
37
+ fi
38
+
39
+ echo "Database import completed successfully."
40
+ else
41
+ echo "Skipping database import."
42
+ fi
run.sh ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # 确保以 postgres 用户身份运行
4
+ if [ "$(id -u)" -ne 1000 ]; then
5
+ echo "切换到 UID 为 1000 的 postgres 用户运行"
6
+ exec gosu postgres "$0" "$@"
7
+ fi
8
+
9
+ # 启动 PostgreSQL 服务
10
+ docker-entrypoint.sh postgres &
11
+
12
+ # 检查 PostgreSQL 服务是否已启动
13
+ echo "等待 PostgreSQL 服务启动..."
14
+ until pg_isready -h localhost; do
15
+ sleep 5
16
+ done
17
+ echo "PostgreSQL 服务已启动!"
18
+
19
+ # export N8N_USER_FOLDER=${DATA_DIR}
20
+ export N8N_ENCRYPTION_KEY="n8n8n8n"
21
+ # Allows usage of all builtin modules
22
+ export NODE_FUNCTION_ALLOW_BUILTIN=*
23
+ # Allow usage of external npm modules.
24
+ export NODE_FUNCTION_ALLOW_EXTERNAL=*
25
+
26
+ # 启动 n8n
27
+ exec n8n