n8n / Dockerfile
hijnu's picture
Update Dockerfile
bb7a720 verified
raw
history blame
3.25 kB
# 基础构建阶段:使用 Node.js 镜像并安装 Python3 和虚拟环境
FROM node:20-slim AS builder
# 设置构建时变量
ARG requirements="requests yt-dlp"
ARG PACKAGES="n8n"
ARG WORKDIR=/app
ARG DUMP_URL
ARG DUMP_PASSWORD
# 安装 Python3、venv 以及必要的系统工具
RUN apt-get update && apt-get install -y python3 python3-venv curl gnupg build-essential && \
npm install -g ${PACKAGES} && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# 设置 Python 虚拟环境
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV && \
$VIRTUAL_ENV/bin/pip install --upgrade pip && \
$VIRTUAL_ENV/bin/pip install $requirements
# 主运行阶段:基于较小的 PostgreSQL 镜像
FROM postgres:latest
# 设置构建时变量
ARG POSTGRES_USER=n8n
ARG POSTGRES_PASSWORD=n8n
ARG POSTGRES_DB=n8n
ARG WEBHOOK_URL=https://aigenai-db.hf.space/
ARG WORKDIR=/app
ARG DB_IMPORT=no
ARG NODEJS_VER=20
ARG PACKAGES="n8n"
# 设置环境变量
ENV POSTGRES_USER=${POSTGRES_USER} \
POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
POSTGRES_DB=${POSTGRES_DB} \
WEBHOOK_URL=${WEBHOOK_URL} \
WORKDIR=${WORKDIR} \
DB_IMPORT=${DB_IMPORT} \
N8N_HOST=0.0.0.0 \
N8N_PORT=7860 \
N8N_PROTOCOL=https \
GENERIC_TIMEZONE=Asia/Shanghai \
N8N_METRICS=true \
QUEUE_HEALTH_CHECK_ACTIVE=true \
N8N_PAYLOAD_SIZE_MAX=256 \
DB_TYPE=postgresdb \
DB_POSTGRESDB_HOST=localhost \
DB_POSTGRESDB_PORT=5432 \
DB_POSTGRESDB_USER=${POSTGRES_USER} \
DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD} \
DB_POSTGRESDB_DATABASE=${POSTGRES_DB} \
VIRTUAL_ENV=/opt/venv \
PATH="$VIRTUAL_ENV/bin:/usr/local/lib/node_modules/n8n/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
# 复制构建阶段的 Node.js 和 n8n
COPY --from=builder /usr/local/bin/node /usr/local/bin/
COPY --from=builder /usr/local/lib/node_modules /usr/local/lib/node_modules
# 复制构建阶段的 Python 运行环境
COPY --from=builder $VIRTUAL_ENV $VIRTUAL_ENV
# 安装必要的软件包并设置时区
RUN apt-get update && apt-get install -y \
curl unzip gnupg build-essential sudo vim git procps lsof net-tools \
ca-certificates openssl tzdata python3-venv gosu \
htop jq wget && \
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
dpkg-reconfigure --frontend noninteractive tzdata
# 安装 rclone
RUN curl https://rclone.org/install.sh | bash
# 设置工作目录并复制启动脚本
WORKDIR ${WORKDIR}
COPY run.sh ${WORKDIR}/run.sh
COPY import-db.sh /docker-entrypoint-initdb.d/
RUN chmod +x ${WORKDIR}/run.sh /docker-entrypoint-initdb.d/import-db.sh
# 更改现有的 postgres 用户 UID 和 GID 为 1000
USER root
RUN usermod -u 1000 postgres && groupmod -g 1000 postgres && \
chown -R postgres:postgres /var/lib/postgresql && chown -R postgres:postgres /var/run/postgresql && \
chown -R postgres:postgres $WORKDIR && mkdir -p $WORKDIR/backups && chmod -R 775 $WORKDIR/backups
# 切换到 postgres 用户
USER postgres
# 健康检查配置(可选)
HEALTHCHECK --interval=120s --timeout=10s --start-period=10s --retries=3 \
CMD curl -f http://localhost:7860/HEALTHZ || exit 1
# 启动容器时执行 run.sh 脚本
CMD ["./run.sh"]