aigenai commited on
Commit
19b5f9c
·
1 Parent(s): ed15814
Files changed (3) hide show
  1. Dockerfile +92 -0
  2. import-db.sh +42 -0
  3. run.sh +33 -0
Dockerfile ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 基础构建阶段:使用 Node.js 镜像并安装 Python3 和虚拟环境
2
+ FROM node:20-slim AS builder
3
+
4
+ # 设置构建时变量
5
+ ARG requirements=requests
6
+ ARG PACKAGES=n8n
7
+ ARG WORKDIR=/app
8
+ ARG DUMP_URL
9
+ ARG DUMP_PASSWORD
10
+
11
+ # 安装 Python3、venv 以及必要的系统工具
12
+ RUN apt-get update && apt-get install -y python3 python3-venv curl gnupg build-essential && \
13
+ npm install -g ${PACKAGES} && \
14
+ apt-get clean && rm -rf /var/lib/apt/lists/*
15
+
16
+ # 设置 Python 虚拟环境
17
+ ENV VIRTUAL_ENV=/opt/venv
18
+ RUN python3 -m venv $VIRTUAL_ENV && \
19
+ $VIRTUAL_ENV/bin/pip install --upgrade pip && \
20
+ $VIRTUAL_ENV/bin/pip install $requirements
21
+
22
+ # 主运行阶段:基于较小的 PostgreSQL 镜像
23
+ FROM postgres:latest
24
+
25
+ # 设置构建时变量
26
+ ARG POSTGRES_USER=n8n
27
+ ARG POSTGRES_PASSWORD=n8n
28
+ ARG POSTGRES_DB=n8n
29
+ ARG WEBHOOK_URL=https://aigenai-db.hf.space/
30
+ ARG WORKDIR=/app
31
+ ARG DB_IMPORT=no
32
+ ARG NODEJS_VER=20
33
+ ARG PACKAGES=n8n
34
+
35
+ # 设置环境变量
36
+ ENV POSTGRES_USER=${POSTGRES_USER} \
37
+ POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
38
+ POSTGRES_DB=${POSTGRES_DB} \
39
+ WEBHOOK_URL=${WEBHOOK_URL} \
40
+ WORKDIR=${WORKDIR} \
41
+ DB_IMPORT=${DB_IMPORT} \
42
+ N8N_HOST=0.0.0.0 \
43
+ N8N_PORT=7860 \
44
+ N8N_PROTOCOL=https \
45
+ GENERIC_TIMEZONE=Asia/Shanghai \
46
+ N8N_METRICS=true \
47
+ QUEUE_HEALTH_CHECK_ACTIVE=true \
48
+ N8N_PAYLOAD_SIZE_MAX=256 \
49
+ DB_TYPE=postgresdb \
50
+ DB_POSTGRESDB_HOST=localhost \
51
+ DB_POSTGRESDB_PORT=5432 \
52
+ DB_POSTGRESDB_USER=${POSTGRES_USER} \
53
+ DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD} \
54
+ DB_POSTGRESDB_DATABASE=${POSTGRES_DB} \
55
+ VIRTUAL_ENV=/opt/venv \
56
+ PATH="$VIRTUAL_ENV/bin:/usr/local/lib/node_modules/n8n/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
57
+
58
+ # 复制构建阶段的 Node.js 和 n8n
59
+ COPY --from=builder /usr/local/bin/node /usr/local/bin/
60
+ COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
61
+ # 复制构建阶段的 Python 运行环境
62
+ COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV
63
+
64
+ # 安装必要的软件包并设置时区
65
+ RUN apt-get update && apt-get install -y \
66
+ curl unzip gnupg build-essential sudo vim git procps lsof net-tools \
67
+ ca-certificates openssl tzdata python3-venv gosu \
68
+ htop jq wget && \
69
+ ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
70
+ dpkg-reconfigure --frontend noninteractive tzdata
71
+
72
+ # 设置工作目录并复制启动脚本
73
+ WORKDIR ${WORKDIR}
74
+ COPY run.sh ${WORKDIR}/run.sh
75
+ COPY import-db.sh /docker-entrypoint-initdb.d/
76
+ RUN chmod +x ${WORKDIR}/run.sh /docker-entrypoint-initdb.d/import-db.sh
77
+
78
+ # 更改现有的 postgres 用户 UID 和 GID 为 1000
79
+ USER root
80
+ RUN usermod -u 1000 postgres && groupmod -g 1000 postgres && \
81
+ chown -R postgres:postgres /var/lib/postgresql && chown -R postgres:postgres /var/run/postgresql && \
82
+ chown -R postgres:postgres $WORKDIR && mkdir -p $WORKDIR/backups && chmod -R 775 $WORKDIR/backups
83
+
84
+ # 切换到 postgres 用户
85
+ USER postgres
86
+
87
+ # 健康检查配置(可选)
88
+ HEALTHCHECK --interval=120s --timeout=10s --start-period=10s --retries=3 \
89
+ CMD curl -f http://localhost:7860/HEALTHZ || exit 1
90
+
91
+ # 启动容器时执行 run.sh 脚本
92
+ CMD ["./run.sh"]
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,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 3
16
+ done
17
+ echo "PostgreSQL 服务已启动!"
18
+
19
+ # 执行数据库导入脚本
20
+ echo "运行数据库导入脚本..."
21
+ /app/import-db.sh
22
+
23
+ sleep 3
24
+
25
+ # export N8N_USER_FOLDER=${DATA_DIR}
26
+ export N8N_ENCRYPTION_KEY="n8n8n8n"
27
+ # Allows usage of all builtin modules
28
+ export NODE_FUNCTION_ALLOW_BUILTIN=*
29
+ # Allow usage of external npm modules.
30
+ export NODE_FUNCTION_ALLOW_EXTERNAL=*
31
+
32
+ # 使用绝对路径调用 n8n
33
+ exec n8n