How to Self-Host n8n with Docker Compose
How to Self-Host n8n with Docker Compose

How to Self-Host n8n with Docker Compose

Introduction

n8n is an open-source workflow automation tool that allows you to automate tasks and integrate various applications, services, and systems. By using n8n, you can create workflows that connect different applications and automate complex tasks. In this article, we will discuss how to self-host n8n using Docker Compose.

Prerequisites

Before we begin, you should have the following prerequisites:

  1. Docker and Docker Compose installed on your system.
  2. Basic knowledge of Docker Compose.

Step 1: Create the Docker Compose File

To start, create a new directory and create a new docker-compose.yaml file with the following content:

version: '3.8'

services:
  n8n:
    image: n8nio/n8n
    ports:
      - "5678:5678"
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=user
      - N8N_BASIC_AUTH_PASSWORD=password
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=n8n
      - DB_POSTGRESDB_USER=n8n
      - DB_POSTGRESDB_PASSWORD=n8n
    depends_on:
      - db
    restart: unless-stopped

  db:
    image: postgres:12
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=n8n
      - POSTGRES_DB=n8n
    restart: unless-stopped

volumes:
  db-data:

This file will create two services: n8n and db. The n8n service runs the n8n image and exposes port 5678. The db service runs the PostgreSQL 12 image and creates a new database for n8n.

You can customize the environment variables for n8n and PostgreSQL to fit your needs.

Step 2: Run Docker Compose

To start n8n, run the following command inside the directory where your docker-compose.yaml file is located:

docker-compose up -d

This command will start n8n and PostgreSQL in the background.

Step 3: Access n8n

Once n8n is running, you can access it by visiting http://localhost:5678 in your web browser. If you enabled basic authentication, you will be prompted to enter a username and password.

Step 4: Stop Docker Compose

To stop n8n and PostgreSQL, run the following command inside the directory where your docker-compose.yaml file is located:

docker-compose down

This command will stop and remove the n8n and PostgreSQL containers.

Conclusion

n8n is a powerful workflow automation tool that can help you automate complex tasks and integrate different applications and services. By self-hosting n8n with Docker Compose, you have full control over your data and can ensure that your workflows are secure and reliable. If you’re interested in using n8n, follow the steps in this article to get started with self-hosting using Docker Compose.