Setting up n8n automation with Docker behind Traefik
Workflow automation with n8n. This guide describes setting it up with Docker and Traefik as a reverse proxy.
Table of Contents
CAUTION
Please note that this blog post was originally written in German and has been translated for your convenience. Although every effort has been made to ensure accuracy, there may be translation errors. I apologize for any discrepancies or misunderstandings that may result from the translation and I am grateful for any corrections in the comments or via mail.
This time, I want to create workflows for automation. This is possible, for example, with n8n.
n8n is an extendable workflow automation tool which enables you to connect anything to everything via its open, fair-code model.
To do this, we need a server with Docker and, in this case, Traefik as a reverse proxy, as described here. The setup is done in a few steps.
Installation n8n
We connect to the server and expand the environment configuration.
cd ~/docker
echo "N8N_POSTGRES_PASSWORD=$(openssl rand -hex 16)" >> .env
echo "N8N_USER=username" >> .env
echo "N8N_PASSWORD=securepassword" >> .env
Now we need new folders in which the data will be stored.
mkdir appdata/n8n
mkdir appdata/n8n/data
mkdir appdata/n8n/db
And a new docker-compose file.
cd ~/docker
nano docker-compose-n8n.yml
version: "3.7"
### NETWORKS ###
networks:
web:
external:
name: web
n8n-internal:
external: false
default:
driver: bridge
### SERVICEs ###
services:
n8n:
container_name: n8n
image: n8nio/n8n
restart: unless-stopped
networks:
- n8n-internal
- web
security_opt:
- no-new-privileges:true
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_HOST=n8n-db
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=${N8N_POSTGRES_PASSWORD}
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=${N8N_USER}
- N8N_BASIC_AUTH_PASSWORD=${N8N_PASSWORD}
- N8N_HOST=n8n.${DOMAINNAME}
- WEBHOOK_TUNNEL_URL=https://n8n.${DOMAINNAME}
- GENERIC_TIMEZONE=${TZ}
- TZ=${TZ}
volumes:
- $DOCKERDIR/appdata/n8n/data:/home/node/
labels:
- "traefik.enable=true"
- "traefik.http.routers.n8n-rtr.entrypoints=https"
- "traefik.http.routers.n8n-rtr.rule=Host(`n8n.$DOMAINNAME`)"
- "traefik.http.routers.n8n-rtr.tls=true"
- "traefik.http.routers.n8n-rtr.service=n8n-svc"
- "traefik.http.services.n8n-svc.loadbalancer.server.port=5678"
- "traefik.http.routers.n8n-rtr.middlewares=chain-oauth@file"
depends_on:
- n8n-db
n8n-db:
container_name: n8n-db
image: postgres
restart: unless-stopped
networks:
- n8n-internal
security_opt:
- no-new-privileges:true
environment:
- POSTGRES_USER=n8n
- POSTGRES_PASSWORD=${N8N_POSTGRES_PASSWORD}
- POSTGRES_DB=n8n
- POSTGRES_NON_ROOT_USER=n8n
- POSTGRES_NON_ROOT_PASSWORD=${N8N_POSTGRES_PASSWORD}
volumes:
- $DOCKERDIR/appdata/n8n/db/:/var/lib/postgresql/data
labels:
- "traefik.enable=false"
When the page n8n.yourdomain.de is called up, a query asks for the username and password in the .env file.
Workflows can now be created.
Example workflow
Now, I would like to create an example workflow. This should send a daily email at the end with the percentage of disk space used in per cent. I add a new “Execute Command” node to the start field.
As a command, I add df -k / | tail -1 | awk '{print $5}'
and press “Execute Node”.
I see that 4% of the disk space is occupied.
Next, I add a new “Send Email” node. The SMTP data must be entered here.
For the text, I click on the cogwheel next to the text field and select “Add Expression”.
The server's memory space is occupied by {{$json["stdout"]}}.
You can also test whether a mail is sent via “Execute Node”.
Successful workflow](@assets/images/blog/n8n-mit-traefik/images/success.png)
So that you don’t always have to run the workflow manually, I add a cron trigger. You can set how often everything should be executed.
I connect this trigger to the node and press play.
Now, I just have to save the workflow at the top right and set it to active.
I should get an email every week with the used disk space.