# 1. Use Node 20 Slim (Debian) FROM node:20-slim # --- CACHE BUSTER --- # Updated timestamp to force this new logic to deploy ENV N8N_VERSION_FORCE=v2_watchdog_upgrade_dec18 # 2. Install System Tools # We add 'curl' because we need it to check n8n's health locally RUN apt-get update && \ apt-get install -y graphicsmagick git tini postgresql-client curl && \ rm -rf /var/lib/apt/lists/* # 3. Install n8n V2.0.2 (Pinned for Stability) RUN npm cache clean --force && \ npm install -g n8n@2.0.2 # 4. Storage & Permissions RUN mkdir -p /home/node/.n8n && \ chown -R node:node /home/node/.n8n # 5. Switch User USER node # 6. The "Internal Affairs" Watchdog # This script waits for n8n to start, then checks n8n's own health status every 60s. # If n8n reports "503 Database not ready", this script kills the container to force a restart. RUN echo '#!/bin/sh' > /home/node/start.sh && \ echo 'echo "--- Starting n8n V2 with Healthz Monitor ---"' >> /home/node/start.sh && \ echo 'n8n start &' >> /home/node/start.sh && \ echo 'N8N_PID=$!' >> /home/node/start.sh && \ echo 'echo "Waiting 30s for boot..."' >> /home/node/start.sh && \ echo 'sleep 30' >> /home/node/start.sh && \ echo 'while true; do' >> /home/node/start.sh && \ echo ' sleep 60' >> /home/node/start.sh && \ echo ' # Check n8n internal health endpoint' >> /home/node/start.sh && \ echo ' if ! curl -f http://localhost:7860/healthz > /dev/null 2>&1; then' >> /home/node/start.sh && \ echo ' echo "❌ n8n is sick (503/Crash)! Killing process to force restart..."' >> /home/node/start.sh && \ echo ' kill $N8N_PID' >> /home/node/start.sh && \ echo ' exit 1' >> /home/node/start.sh && \ echo ' fi' >> /home/node/start.sh && \ echo 'done' >> /home/node/start.sh && \ echo 'wait $N8N_PID' >> /home/node/start.sh && \ chmod +x /home/node/start.sh # 7. V2 Configuration Variables ENV DB_TYPE=postgresdb ENV DB_POSTGRESDB_SCHEMA=public ENV N8N_PORT=7860 ENV N8N_HOST=0.0.0.0 ENV N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=false ENV N8N_RUNNERS_ENABLED=true ENV N8N_BLOCK_ENV_ACCESS_IN_NODE=true # 8. Open Port EXPOSE 7860 # 9. Start ENTRYPOINT ["/usr/bin/tini", "--"] CMD ["/home/node/start.sh"]