Published on

How to setup n8n with docker

Authors

how to setup n8n with docker

n8n is an open-source workflow automation tool that helps automate repetitive tasks, connect various services, and streamline your workflows. It can be easily set up using Docker, allowing for fast deployment without the need to manually configure environments. Docker is an excellent option because it encapsulates n8n and all of its dependencies in a container, ensuring a consistent and reliable setup across different environments.

In this article, we'll walk you through the process of setting up n8n using Docker.

Prerequisites

Before you get started, ensure that you have the following:

  • Docker and Docker Compose installed on your system.
  • Basic knowledge of how Docker works and familiarity with command-line operations.

If you don't have Docker installed, follow the installation guides for your operating system:

Want more Docker and automation tutorials?

Step 1: Pull the n8n Docker Image

n8n provides an official Docker image that we will use to set up the application. You can easily pull the image using the following command:

docker pull n8nio/n8n:latest

This will download the latest stable version of n8n from Docker Hub.

Step 2: Set Up Docker Compose File

While you can run n8n with just a Docker command, Docker Compose is a better option to manage multiple services (like a database) and their interactions.

Create a new directory for your n8n setup:
mkdir n8n-docker
cd n8n-docker

Create a docker-compose.yml file in this directory:

services:
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: always
    env_file:
      - .env
    ports:
      - 5678:5678
    volumes:
      - n8n_data:/home/node/.n8n

volumes:
  n8n_data:

Create a .env file in the same directory:

DB_TYPE=postgresdb
DB_POSTGRESDB_HOST=n8n_postgres
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=${DB_POSTGRESDB_PASSWORD}
DB_POSTGRESDB_DATABASE=n8n

# Email Configuration (for SMTP)
N8N_EMAIL_MODE=smtp
N8N_SMTP_HOST=n8n.smtp.host
N8N_SMTP_PORT=587
N8N_SMTP_USER=${N8N_SMTP_USER}
N8N_SMTP_PASS=${N8N_SMTP_PASS}
N8N_SMTP_SENDER=${N8N_SMTP_SENDER_EMAIL}
N8N_SMTP_SSL=false
# N8N_SMTP_SSL=true # Set to false only if using STARTTLS

# Basic Authentication (for securing n8n)
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=${N8N_BASIC_AUTH_USER}
N8N_BASIC_AUTH_PASSWORD=${N8N_BASIC_AUTH_PASSWORD}

# Timezone and Encryption Settings
GENERIC_TIMEZONE=Europe/Berlin
N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}

N8N_TRUST_PROXY=true
N8N_PROXY_HOPS=1

# n8n Host and Port Settings
N8N_HOST=n8n.yourdomain.com
N8N_PORT=5678

# Webhook URL (ensure this is set to your production URL in a live environment)
WEBHOOK_URL=https://n8n.yourdomain.com/webhook

Step 3: Start n8n with Docker Compose

In the same directory as your docker-compose.yml, run the following command to start the services:

docker-compose up -d

Once the services are up and running, you can access n8n by navigating to http://localhost:5678 in your browser.

Step 4: Configure your nginx file

If you want to use a reverse proxy, here's a sample nginx configuration:

server {
    listen 80;
    server_name n8n.yourdomain.com;
    location / {
        proxy_pass http://localhost:5678;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_read_timeout 3600;
        proxy_send_timeout 3600;
        proxy_buffering off;
        client_max_body_size 100M;
    }
}

Step 5: Access n8n Web Interface

After running the above command, you can access the n8n instance via the web interface at:

https://n8n.yourdomain.com

Authentication

  • Username: admin (as set in the N8N_BASIC_AUTH_USER environment variable).
  • Password: adminpassword (as set in the N8N_BASIC_AUTH_PASSWORD environment variable).

Once logged in, you can begin creating workflows and automating tasks!

Step 6: Stopping and Restarting n8n

To stop the containers, you can run:

docker-compose down

This will stop the containers and remove them, but the data will still persist thanks to the Docker volume n8n_data.

If you want to restart the containers (after stopping them), use:

docker-compose up -d

Step 7: Scaling and Customization

Scaling n8n

If you want to run multiple instances of n8n for load balancing, you can modify your docker-compose.yml file to scale the n8n service. For example, to scale n8n to 2 instances:

n8n:
  image: n8nio/n8n
  container_name: n8n
  deploy:
    replicas: 2

Customizing n8n

You can customize the behavior of your n8n instance in the following ways:

  • Storage: Modify the database settings to use a different database (e.g., MySQL) instead of PostgreSQL.
  • Time Zone: Change the GENERIC_TIMEZONE environment variable to your desired timezone.
  • Add Custom Credentials: If you're using external APIs, you can add custom credentials via the n8n interface.

Step 8: Updating n8n

To update your n8n installation to the latest version, follow these steps:

  1. Pull the latest version of the Docker image:

    docker pull n8nio/n8n:latest
    
  2. Restart the containers:

    docker-compose down
    docker-compose up -d
    

This will restart the services with the new image version.

Want more Docker and automation tutorials?

Conclusion

Setting up n8n with Docker is straightforward and allows for quick deployment, flexibility, and easy scaling. By following the steps outlined in this article, you now have a working n8n instance that you can use to automate workflows, integrate services, and save time on repetitive tasks.

With Docker and Docker Compose, you get the added benefit of easily managing your n8n instance, keeping your configuration portable, and ensuring that the system is isolated from other software on your machine.

You're now ready to automate your workflows securely and reliably with n8n and Docker. 🚀