ain8n / Dockerfile
abidkhan03's picture
fix: upgrade the base image from nodejs20 → nodejs22.
967a040 verified
Raw
History Blame Contribute Delete
6.38 kB
# 使用 PostgreSQL 作为基础镜像
FROM nikolaik/python-nodejs:python3.12-nodejs22
# 添加维护者信息
LABEL maintainer="ai来事 <https://www.youtube.com/@all.ai.>"
ARG CACHEBUST=2
# 设置构建参数,提供默认值
ARG WEBHOOK_URL=https://你的hf账户名-空间名.hf.space/
ARG POSTGRESDB_PORT=5432
# 设置基本环境变量
ENV WEBHOOK_URL=${WEBHOOK_URL} \
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 \
N8N_LOG_LEVEL=info \
EXECUTIONS_DATA_MAX_AGE=168 \
EXECUTIONS_DATA_PRUNE=true \
N8N_EXECUTE_IN_PROCESS=false \
DB_TYPE=postgresdb \
DB_POSTGRESDB_PORT=${POSTGRESDB_PORT} \
VIRTUAL_ENV=/home/pn/venv \
PATH="/home/pn/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" \
# 添加 Redis 配置
QUEUE_BULL_REDIS_HOST=localhost \
QUEUE_BULL_REDIS_PORT=6379 \
QUEUE_BULL_REDIS_DB=0 \
N8N_QUEUE_BULL_REDIS_PREFIX=n8n:queue \
EXECUTIONS_MODE=regular \
# 添加 Qdrant 配置
QDRANT_HOST=http://localhost \
QDRANT_PORT=6333 \
QDRANT_VERSION=1.12.4 \
# 添加超时配置
WAIT_TIMEOUT=30 \
DB_POSTGRESDB_SSL_REJECT_UNAUTHORIZED=false \
N8N_DISABLE_TASK_BROKER=false \
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=false \
# Skip webhook deregistration on shutdown for cleaner restarts
N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN=true \
# Disable worker offloading since we're using regular mode
OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS=false \
# ── Fix: force node-gyp to always use system Python, never the venv ──
# This is the key line that makes native addon compilation future-proof.
# No matter what Python version or venv state exists, node-gyp will
# always use the system Python which has setuptools installed via apt.
NODE_GYP_FORCE_PYTHON=/usr/bin/python3
# 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
unzip \
gnupg \
build-essential \
sudo \
vim \
git \
procps \
lsof \
net-tools \
ca-certificates \
openssl \
tzdata \
htop \
jq \
netcat-openbsd \
redis-server \
# ── Fix: install setuptools and python3-dev system-wide ──────────────
# python3-setuptools: restores 'distutils' for Python 3.12+ which
# removed it from stdlib. Required by node-gyp for native addons.
# python3-dev: provides Python C headers needed for any native builds.
# Being apt-managed means it auto-updates with OS and stays compatible
# with whatever Python version the base image ships in the future.
python3-setuptools \
python3-dev \
&& ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& dpkg-reconfigure --frontend noninteractive tzdata \
\
# ── Qdrant ────────────────────────────────────────────────────────────
&& cd /tmp \
&& curl -L https://github.com/qdrant/qdrant/releases/download/v${QDRANT_VERSION}/qdrant-x86_64-unknown-linux-gnu.tar.gz -o qdrant.tar.gz \
&& tar xvf qdrant.tar.gz \
&& mv qdrant /usr/local/bin/ \
&& rm qdrant.tar.gz \
&& mkdir -p /home/pn/.n8n/qdrant/storage \
&& mkdir -p /home/pn/.n8n/qdrant/config \
&& mkdir -p /home/pn/.n8n/qdrant/snapshots \
&& chown -R pn:pn /home/pn/.n8n/qdrant \
&& chmod -R 755 /home/pn/.n8n/qdrant \
\
# ── Node / n8n ─────────────────────────────────────────────────────────
# IMPORTANT: npm install runs BEFORE venv creation.
# If venv existed first, node-gyp could pick up venv's Python
# (which has no distutils) even with NODE_GYP_FORCE_PYTHON set.
# Correct order: npm → venv (never the other way around).
#
# node-gyp is pre-installed globally so it's ready for any native
# addon that n8n@latest or future versions might include.
&& npm install -g node-gyp \
&& npm install -g --force n8n@latest pnpm \
&& npm cache clean --force \
\
# ── Python venv ────────────────────────────────────────────────────────
# Created AFTER npm so the venv Python is never in scope during
# node-gyp builds. setuptools is also installed inside the venv
# so pip itself and any venv-based tools stay future-proof too.
&& python3 -m venv ${VIRTUAL_ENV} \
&& ${VIRTUAL_ENV}/bin/pip install --no-cache-dir --upgrade \
pip \
setuptools \
requests \
yt-dlp \
youtube-transcript-api \
\
# ── Cleanup ────────────────────────────────────────────────────────────
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# 创建环境变量文件
RUN --mount=type=secret,id=POSTGRES_USER,mode=0444,required=true \
--mount=type=secret,id=POSTGRES_PASSWORD,mode=0444,required=true \
--mount=type=secret,id=POSTGRES_DB,mode=0444,required=true \
--mount=type=secret,id=POSTGRESDB_HOST,mode=0444,required=true \
echo "export DB_POSTGRESDB_HOST=$(cat /run/secrets/POSTGRESDB_HOST)" >> /home/pn/.env && \
echo "export DB_POSTGRESDB_USER=$(cat /run/secrets/POSTGRES_USER)" >> /home/pn/.env && \
echo "export DB_POSTGRESDB_PASSWORD=$(cat /run/secrets/POSTGRES_PASSWORD)" >> /home/pn/.env && \
echo "export DB_POSTGRESDB_DATABASE=$(cat /run/secrets/POSTGRES_DB)" >> /home/pn/.env && \
chown pn:pn /home/pn/.env
# 创建工作目录
WORKDIR /home/pn/n8n
# 复制启动脚本并创建数据目录
COPY --chown=pn:pn run.sh ./run.sh
COPY --chown=pn:pn config/n8n_env.sh ./config/n8n_env.sh
RUN chmod +x ./run.sh \
&& mkdir -p /home/pn/.n8n \
&& chown -R pn:pn /home/pn/.n8n \
&& chown -R pn:pn /home/pn/n8n
# 暴露端口
EXPOSE 7860
# 切换到非 root 用户
USER pn
# 设置数据卷
VOLUME ["/home/pn/.n8n"]
# 启动命令
CMD ["./run.sh"]