Tired of juggling apps and manual tasks?

Self-hosting n8n could be your game-changer. It’s a powerful, open-source automation tool, and setting it up yourself using Docker Compose and Traefik makes it both flexible and super accessible. I'll walk you through the setup and show off the cool AI features that really make n8n shine.

So, what exactly is n8n?

Think of n8n as your digital duct tape. It's a free, open-source tool that lets you visually connect different apps and services to automate just about any workflow you can imagine. Drag and drop nodes to build sequences that handle all those boring, repetitive tasks for you. It saves a ton of time and gives you way more control over your data.

n8n's Secret Sauce: AI Power

n8n isn't just about simple "if this, then that" rules anymore. It's evolved into a legit platform for building smart, AI-driven applications. How? By using something called "agentic AI." This means your workflows can actually make decisions, understand context, and interact with your data intelligently, not just follow a rigid script.

Here’s what makes the AI side so powerful:

  1. The AI Agent Node: This is the brains of the operation. It processes info, makes choices, and uses built-in "tools" to talk to other apps or data sources. It connects to big AI models like OpenAI's GPT, Google's Gemini, or Anthropic's Claude to understand what you need and give smart replies.
  2. LangChain Built Right In: n8n plays nicely with LangChain, a popular framework for building complex AI logic. This means you can create chains of AI actions or even agents that decide which actions to take next, all within your workflow.
  3. Talk to Your Data (Vector DBs): Connect n8n to vector databases like Pinecone or Supabase, or use its simple built-in memory store. This is key for building RAG systems – where the AI can pull answers from your own private docs or data, making it super relevant and useful.
  4. Endless Possibilities: With these tools, you can build things like: smart chatbots for customers, AI assistants that summarize your emails and book meetings, or systems that dig insights out of messy, unstructured data.

What You'll Need Before We Start

Don't sweat it, it's mostly standard gear for self-hosters:

The Docker Compose Setup

We'll use a compose.yaml file to spin up both n8n and its database (we're using PostgreSQL here). Traefik integration is handled smoothly with labels right in the file – it takes care of routing traffic and HTTPS automatically.


Here's the compose.yaml file:

version: '3.8'

services:
  n8n:
    image: n8nio/n8n
    container_name: n8n
    restart: unless-stopped
    ports:
      - "127.0.0.1:5678:5678" # Only exposed locally, Traefik handles external access
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}  # Comes from .env file
      - DB_POSTGRESDB_USER=${POSTGRES_USER}     # Comes from .env file
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD} # Comes from .env file
      - NODE_FUNCTION_ALLOW_EXTERNAL=langchain # Important for LangChain nodes!
    volumes:
      - n8n_data:/home/node/.n8n # Persist your workflows and settings
    networks:
      - n8n
      - traefik
    depends_on:
      - postgres
    labels: # Traefik magic happens here
      - "traefik.enable=true"
      - "traefik.http.routers.n8n.rule=Host(`n8n.yourdomain.com`)" # <<< REPLACE WITH YOUR DOMAIN!
      - "traefik.http.routers.n8n.entrypoints=websecure"
      - "traefik.http.routers.n8n.tls.certresolver=myresolver" # <<< REPLACE 'myresolver' with your Traefik resolver name!
      - "traefik.http.services.n8n.loadbalancer.server.port=5678"

  postgres:
    image: postgres:14
    container_name: n8n_postgres
    restart: unless-stopped
    environment:
      - POSTGRES_DB=${POSTGRES_DB}      # From .env
      - POSTGRES_USER=${POSTGRES_USER}    # From .env
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} # From .env
    volumes:
      - postgres_data:/var/lib/postgresql/data # Persist your database
    networks:
      - n8n

volumes:
  n8n_data:
  postgres_data:

networks:
  n8n:
  traefik:
    external: true # Assumes your Traefik network already exists


Don't forget the .env file!

Create a .env file in the same folder as your compose.yaml to keep your database secrets safe:

POSTGRES_DB=n8n
POSTGRES_USER=n8nuser
POSTGRES_PASSWORD=a_very_strong_and_secret_password  # Make it a good one!

Let's Get n8n Running!

  1. Make a Home: Create a directory for your n8n setup and save both the compose.yaml and .env files inside it.
  2. Traefik Takes Over: Traefik should automatically spot your new service, grab an SSL certificate, and start routing traffic to https://n8n.yourdomain.com.
  3. Almost There! Once the containers are running (give it a minute after the command finishes), open your domain in a web browser. The n8n web interface will greet you and guide you through the initial user setup.
  4. Breathe Easy: Notice the n8n port (5678) is only exposed locally on your server. Traefik is the secure gatekeeper handling all outside traffic. We can go even further. On my server, I don't even expose that port internally, as it's not needed and better not to expose things we don't need.


Fire it Up: Open a terminal, navigate to that directory, and run:

docker compose up -d


That's it! Your self-hosted n8n automation playground is ready. Go build something awesome!


Want to learn how to secure your database? I have an article about that:

https://danielpetrica.com/easy-database-backups-with-docker-compose/


If N8N is not your sauce, active pieces is actually a great, compelling alternative tool

https://danielpetrica.com/installing-activepieces-with-docker-compose-and-traefik/


Photo by Steve Johnson / Unsplash