# 使用 PostgreSQL 作为基础镜像 FROM postgres:15 # 设置构建时变量,确保为每个变量提供默认值 ARG DUMP_URL="" ARG DUMP_PASSWORD="" 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=yes ARG NODEJS_VER=20 ARG WEBDAV_URL="https://cfr2.n8n.us.kg/" ARG WEBDAV_USER="your_username" ARG WEBDAV_PASSWORD="your_password" # 设置环境变量 ENV POSTGRES_USER=${POSTGRES_USER} \ POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \ POSTGRES_DB=${POSTGRES_DB} \ WEBHOOK_URL=${WEBHOOK_URL} \ 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=$WORKDIR/venv \ WEBDAV_URL=${WEBDAV_URL} \ WEBDAV_USER=${WEBDAV_USER} \ WEBDAV_PASSWORD=${WEBDAV_PASSWORD} \ WORKDIR=${WORKDIR} \ PATH="$WORKDIR/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" COPY requirements.txt ${WORKDIR}/requirements.txt COPY package.json ${WORKDIR}/package.txt # 安装必要的软件包、Node.js,Python,并设置时区,清理 apt 缓存以减少镜像大小 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 python3-venv python3-pip gosu \ htop jq wget lftp cron && \ curl -fsSL https://deb.nodesource.com/setup_${NODEJS_VER}.x | bash - && \ apt-get install -y nodejs && \ ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ dpkg-reconfigure --frontend noninteractive tzdata && \ curl https://rclone.org/install.sh | bash && \ apt-get clean && rm -rf /var/lib/apt/lists/* && \ python3 -m venv $VIRTUAL_ENV && \ $VIRTUAL_ENV/bin/pip install --upgrade pip && \ $VIRTUAL_ENV/bin/pip install -r ${WORKDIR}/requirements.txt && \ npm install -g $(cat ${WORKDIR}/package.txt) # 设置工作目录并复制脚本 WORKDIR ${WORKDIR} COPY run.sh ${WORKDIR}/run.sh COPY import-db.sh ${WORKDIR}/import-db.sh COPY backup.sh ${WORKDIR}/backup.sh RUN chmod +x ${WORKDIR}/run.sh ${WORKDIR}/import-db.sh ${WORKDIR}/backup.sh # 更改 postgres 用户 UID 和 GID 为 1000,设置备份目录和 crontab 目录权限 USER root RUN usermod -u 1000 postgres && groupmod -g 1000 postgres && \ chown -R postgres:postgres /var/lib/postgresql /var/run/postgresql ${WORKDIR} && \ mkdir -p ${WORKDIR}/backups && chmod -R 775 ${WORKDIR}/backups && \ chmod 1730 /var/spool/cron/crontabs # 设置定期备份任务:每4小时运行一次备份任务 RUN echo "0 */1 * * * /app/backup.sh" > /etc/cron.d/backup && \ chmod 0644 /etc/cron.d/backup && crontab /etc/cron.d/backup # 切换到 postgres 用户 USER postgres # 暴露 n8n 服务端口 EXPOSE 7860 # 启动 cron 服务和运行脚本 CMD service cron start && ./run.sh