Update Dockerfile
Browse files- Dockerfile +101 -102
Dockerfile
CHANGED
@@ -1,102 +1,101 @@
|
|
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 && npm install -g ${PACKAGES} && \
|
16 |
-
apt-get clean && rm -rf /var/lib/apt/lists/*
|
17 |
-
|
18 |
-
# 验证 Node.js 和 npm 安装的 node_modules 路径是否存在
|
19 |
-
RUN ls -al /usr/local/lib/node_modules || echo "node_modules not found!"
|
20 |
-
|
21 |
-
# 设置 Python 虚拟环境
|
22 |
-
ENV VIRTUAL_ENV=/opt/venv
|
23 |
-
RUN python3 -m venv $VIRTUAL_ENV && \
|
24 |
-
$VIRTUAL_ENV/bin/pip install --upgrade pip && \
|
25 |
-
$VIRTUAL_ENV/bin/pip install $requirements
|
26 |
-
|
27 |
-
# 主运行阶段:基于较小的 PostgreSQL 镜像
|
28 |
-
FROM postgres:latest
|
29 |
-
|
30 |
-
# 设置环境变量
|
31 |
-
ARG POSTGRES_USER=n8n
|
32 |
-
ARG POSTGRES_PASSWORD=n8n
|
33 |
-
ARG POSTGRES_DB=n8n
|
34 |
-
ARG WEBHOOK_URL=https://aigenai-db.hf.space/
|
35 |
-
ARG WORKDIR=/app
|
36 |
-
ARG DB_IMPORT=no
|
37 |
-
|
38 |
-
# 设置 n8n 环境变量
|
39 |
-
ENV N8N_HOST=0.0.0.0 \
|
40 |
-
N8N_PORT=7860 \
|
41 |
-
N8N_PROTOCOL=https \
|
42 |
-
WEBHOOK_URL=${WEBHOOK_URL} \
|
43 |
-
GENERIC_TIMEZONE=Asia/Shanghai \
|
44 |
-
N8N_METRICS=true \
|
45 |
-
QUEUE_HEALTH_CHECK_ACTIVE=true \
|
46 |
-
N8N_PAYLOAD_SIZE_MAX=256
|
47 |
-
|
48 |
-
# 设置数据库连接相关的环境变量
|
49 |
-
ENV 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 |
-
|
56 |
-
# 设置 Python 环境变量
|
57 |
-
ENV VIRTUAL_ENV=/opt/venv \
|
58 |
-
PATH="$VIRTUAL_ENV/bin:$PATH"
|
59 |
-
|
60 |
-
# 复制构建阶段的 n8n 和 Python 运行环境
|
61 |
-
COPY --from=builder /usr/local/bin/node /usr/local/bin/node
|
62 |
-
COPY --from=builder /usr/lib/node_modules /usr/lib/node_modules || echo "Skipping node_modules copy, not
|
63 |
-
COPY --from=builder
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
chown -R postgres:postgres /var/
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
CMD ["./run.sh"]
|
|
|
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 && npm install -g ${PACKAGES} && \
|
16 |
+
apt-get clean && rm -rf /var/lib/apt/lists/*
|
17 |
+
|
18 |
+
# 验证 Node.js 和 npm 安装的 node_modules 路径是否存在
|
19 |
+
RUN ls -al /usr/local/lib/node_modules || echo "node_modules not found!"
|
20 |
+
|
21 |
+
# 设置 Python 虚拟环境
|
22 |
+
ENV VIRTUAL_ENV=/opt/venv
|
23 |
+
RUN python3 -m venv $VIRTUAL_ENV && \
|
24 |
+
$VIRTUAL_ENV/bin/pip install --upgrade pip && \
|
25 |
+
$VIRTUAL_ENV/bin/pip install $requirements
|
26 |
+
|
27 |
+
# 主运行阶段:基于较小的 PostgreSQL 镜像
|
28 |
+
FROM postgres:latest
|
29 |
+
|
30 |
+
# 设置环境变量
|
31 |
+
ARG POSTGRES_USER=n8n
|
32 |
+
ARG POSTGRES_PASSWORD=n8n
|
33 |
+
ARG POSTGRES_DB=n8n
|
34 |
+
ARG WEBHOOK_URL=https://aigenai-db.hf.space/
|
35 |
+
ARG WORKDIR=/app
|
36 |
+
ARG DB_IMPORT=no
|
37 |
+
|
38 |
+
# 设置 n8n 环境变量
|
39 |
+
ENV N8N_HOST=0.0.0.0 \
|
40 |
+
N8N_PORT=7860 \
|
41 |
+
N8N_PROTOCOL=https \
|
42 |
+
WEBHOOK_URL=${WEBHOOK_URL} \
|
43 |
+
GENERIC_TIMEZONE=Asia/Shanghai \
|
44 |
+
N8N_METRICS=true \
|
45 |
+
QUEUE_HEALTH_CHECK_ACTIVE=true \
|
46 |
+
N8N_PAYLOAD_SIZE_MAX=256
|
47 |
+
|
48 |
+
# 设置数据库连接相关的环境变量
|
49 |
+
ENV 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 |
+
|
56 |
+
# 设置 Python 环境变量
|
57 |
+
ENV VIRTUAL_ENV=/opt/venv \
|
58 |
+
PATH="$VIRTUAL_ENV/bin:$PATH"
|
59 |
+
|
60 |
+
# 复制构建阶段的 n8n 和 Python 运行环境
|
61 |
+
COPY --from=builder /usr/local/bin/node /usr/local/bin/node
|
62 |
+
COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules || echo "Skipping node_modules copy, not found."
|
63 |
+
COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV
|
64 |
+
|
65 |
+
# 安装必要的软件包
|
66 |
+
RUN apt-get update && apt-get install -y \
|
67 |
+
curl unzip gnupg build-essential sudo vim git procps lsof net-tools \
|
68 |
+
ca-certificates openssl tzdata python3-venv gosu \
|
69 |
+
htop jq wget && \
|
70 |
+
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
71 |
+
dpkg-reconfigure --frontend noninteractive tzdata
|
72 |
+
|
73 |
+
# 设置工作目录并复制启动脚本
|
74 |
+
WORKDIR ${WORKDIR}
|
75 |
+
COPY run.sh ${WORKDIR}/run.sh
|
76 |
+
RUN chmod +x ${WORKDIR}/run.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 && \
|
82 |
+
chown -R postgres:postgres /var/run/postgresql
|
83 |
+
|
84 |
+
# 切换到 postgres 用户,初始化数据库并创建角色
|
85 |
+
USER postgres
|
86 |
+
RUN initdb -D /var/lib/postgresql/data && \
|
87 |
+
pg_ctl start -D /var/lib/postgresql/data && \
|
88 |
+
psql --command "CREATE ROLE $POSTGRES_USER WITH LOGIN SUPERUSER PASSWORD '$POSTGRES_PASSWORD';" && \
|
89 |
+
createdb -O $POSTGRES_USER $POSTGRES_DB && \
|
90 |
+
pg_ctl stop -D /var/lib/postgresql/data
|
91 |
+
|
92 |
+
# 添加条件导入的 shell 脚本
|
93 |
+
COPY import-db.sh /docker-entrypoint-initdb.d/
|
94 |
+
RUN chmod +x /docker-entrypoint-initdb.d/import-db.sh
|
95 |
+
|
96 |
+
# 健康检查配置(可选)
|
97 |
+
HEALTHCHECK --interval=120s --timeout=10s --start-period=10s --retries=3 \
|
98 |
+
CMD curl -f http://localhost:7860/HEALTHZ || exit 1
|
99 |
+
|
100 |
+
# 启动容器时执行run.sh脚本
|
101 |
+
CMD ["./run.sh"]
|
|