DEV Community

IT Solutions Pro
IT Solutions Pro

Posted on

Build a Self-Hosted AI Server (n8n + Docker + OpenAI)

If you are serious about automation in 2026, you have two choices: Pay monthly fees for services like Zapier, or become the architect of your own infrastructure.

Today, we choose option two.

We are building a self-hosted AI server that monitors inputs, analyzes them with GPT-4, and sends intelligent alerts directly to Telegram—all running locally via Docker.

This isn't just a toy project; this is an enterprise-grade solution that you can run on a local machine, a Raspberry Pi, or a cloud VPS for pennies.

Why Self-Host?

  1. Privacy: Your data stays on your machine.
  2. Cost: No more "pay per task" limits like Zapier.
  3. Power: You get full control over the AI logic and database.

Prerequisites

  • Docker Desktop installed.
  • VS Code text editor.
  • An OpenAI API Key.

Step 1: Secure Configuration (.env)

First, we need to set up our environment variables. This keeps our passwords safe and out of the main code.
Create a file named .env:

.env file configuration

POSTGRES_USER=n8n
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_DB=n8n
N8N_ENCRYPTION_KEY=supersecretkey123
N8N_HOST=localhost
N8N_PORT=5678
GENERIC_TIMEZONE=America/New_York
Enter fullscreen mode Exit fullscreen mode

Step 2: The Architecture (docker-compose.yml)
We are using Docker Compose to spin up two containers:

PostgreSQL: A robust database to store workflow history.

n8n: The workflow automation tool itself.

Create a file named docker-compose.yml:

YAML

version: '3.8'

services:
  postgres:
    image: postgres:16-alpine
    restart: always
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data

  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - N8N_HOST=${N8N_HOST}
    links:
      - postgres
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - postgres

volumes:
  postgres_data:
  n8n_data:
Enter fullscreen mode Exit fullscreen mode

How to Run It
Open your terminal in the same folder and run:


docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Then visit http://localhost:5678 in your browser.

Step 3: Programming the AI Agent
To make this system smart, we need to give the AI specific instructions. When setting up the AI Agent Node in n8n, use this System Prompt to enforce "Structured Output" (JSON):

Plaintext
You are a Senior IT Support Automation Agent.
Your job is to analyze incoming text and categorize it.

RULES:

  1. If the text contains words like "crash", "down", "urgent", or "fire", categorize as "HIGH_PRIORITY".
  2. Otherwise, categorize as "LOW_PRIORITY".

OUTPUT FORMAT (JSON ONLY):

{
"category": "HIGH_PRIORITY" | "LOW_PRIORITY",
"suggested_reply": "Write a short, professional response to the user here."
}

Step 4: Connecting Telegram
To receive alerts on your phone:

Search for @botfather on Telegram.

Type /newbot to get your API Token.

Search for @userinfobot to get your personal Chat ID.

Use these credentials in the Telegram Node in n8n.

Conclusion
You have just built a system that would cost enterprise companies thousands of dollars, and you did it for free on your own hardware. That is the power of open-source IT.

In the next tutorial, we will secure this server with SSL and a custom domain.

Happy Automating!

📺 Watch the Full Masterclass

If you prefer text, check out the channel here: @it_solutions_pro

Top comments (0)