Hosting n8n with docker compose and traefik

I self host a lot of software on one single server, jugling all this different software is super easy with docker compose and Traefik, Because of this I decided to share my setup for handling n8n with docker compose and Traefik here.

here’s the most important part:

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"
...

For a complete overview 0please consut my original article here:

https://danielpetrica.com/self-hosting-n8n-with-docker-compose-and-traefik-for-ai-powered-workflow-automation/

1 Like

Thanks for the detailed post! I followed your guide and successfully set up n8n on my VPS. While doing it manually worked, I found it a bit time-consuming to run each command one by one.

To make things easier, I created a script that automates the process — it allows you to install, upgrade, back up, and restore n8n with just a few commands. I’ve shared it here on my GitHub:
https://github.com/thenguyenvn90/n8n

Hopefully this helps others who want a faster setup!

2 Likes

I want to note that setting it up this way open the n8n web UI to the internet. If that is the desired outcome, no problem.

But if one does not need to work on the web UI from the internet, one might consider not exposing everything n8n to the internet and only expose the required incoming trigger URLs. For example, allow just webhooks.

2 Likes

Thank you for the script

Thank you for the note. Exposing n8n to the internet was what I wanted to do. Also I configured cloudflare proxy in front of my server to reduce attack vectors. In my experience a properly configured cloudflare can do wonders in reducing attack vectors even if it doesn’t solve everything obviously.