AI & Automation · Guide

How to build an AI agent workflow with self-hosted n8n without losing your work

Run n8n on infrastructure you own, keep every workflow and credential safe forever, and ship a real AI agent end to end.

Ishan Vats By Ishan Vats · Founder of IV Consulting · builds AI agents & automations for 150+ teams

Jun 2026 12 min read Pillar: AI & Automation
Self-hosted Docker Persistent volume AI Agent node
Self-Hosted n8n · Live
Webhook · TriggerNew request comes in
Claude logo AI Agent · ClaudeReason + draft reply
Notion logo NotionLogged
Slack logo SlackTeam alerted
Gmail logo GmailDraft staged
Zero data losssurvives every reboot
Quick answer

To build an AI agent workflow with self-hosted n8n, run n8n in Docker with a persistent volume so your workflows and credentials are never deleted, then chain a Webhook trigger into an AI Agent node and out to your tools like Notion, Slack, and Gmail. The single most important step most guides skip is persistence: if you run n8n without a named volume, every automation you build is wiped the moment the container stops.

01

What is self-hosted n8n, and why run it yourself

Self-hosted n8n is the fastest way to run real AI automations on infrastructure you own, with no per-execution fees and no vendor lock-in. But there is a trap in almost every "install n8n with Docker" tutorial online, and it costs people hours of lost work. This guide fixes that. You will get n8n running, keep your data safe forever, and build a working AI agent workflow end to end.

n8n is an open source workflow automation tool, similar in spirit to Zapier or Make, but you host it yourself. That difference matters more than it sounds:

  • You own your data. Workflows, credentials, and execution history live on your machine, not a third-party cloud.
  • No per-task billing. Run 100 or 100,000 executions a month at the same cost.
  • Full AI control. Plug in your own Anthropic or OpenAI keys and build true AI agents, not just rigid if-this-then-that rules.

The trade-off is that you are responsible for running it and keeping the data safe. That is exactly what the next section protects you from.

IV Consulting take Self-hosted n8n is the first tool we install for most automation clients. It is the backbone of the systems we build, and the platform our clients keep running independently after we hand over. If you want this built and hardened for you, that is exactly what our Automation stage does.
02

The mistake that deletes all your workflows

Most tutorials tell you to run this:

docker run -it --rm --name n8n -p 5678:5678 n8nio/n8n
Do not use this command It will delete everything you build. The moment the container stops, your workflows and saved credentials are gone with it.

Here is why, in plain terms:

  • --rm tells Docker to destroy the container the moment it stops.
  • No volume means there is nowhere for your data to live outside that container.

So the instant you close the terminal, reboot, or the container restarts, your container is thrown away and there is no saved copy to come back to. Every workflow, every saved credential, gone. This is the most common reason people give up on self-hosted n8n, and it is completely avoidable.

The fix is one concept: a persistent volume. A volume is a storage area that lives outside the container, managed by Docker. Your container can be deleted, upgraded, or recreated a hundred times, and the volume keeps your data intact. Think of the container as a disposable player and the volume as the saved game file.

03

Run self-hosted n8n the way that never loses your work

1

Install Docker

Docker is the engine that runs n8n in a clean, self-contained box, so you do not have to install Node.js or juggle dependencies by hand.

On Mac or Windows: Download Docker Desktop from docker.com, install it, and open it once. On first launch it asks you to accept the terms and enter your password to set up its helper. Let it finish starting. The whale icon in your menu bar or system tray stops animating when it is ready.

On Linux: Install Docker Engine and the Compose plugin from the official docs.

Confirm it works:

docker --version docker compose version

If both print a version number, you are ready.

2

Run n8n the safe way (with persistence)

Instead of a throwaway command, use a small recipe file called docker-compose.yml. It is reusable, readable, and it wires in persistence, auto-restart, and a stable encryption key for your credentials. Create a folder for your project, for example n8n, and inside it create two files.

File 1: .env holds your secret encryption key and timezone:

N8N_ENCRYPTION_KEY=replace-with-a-long-random-string GENERIC_TIMEZONE=Asia/Kolkata TZ=Asia/Kolkata

To generate a strong key, run openssl rand -hex 24 and paste the result in. This key is what unlocks your saved credentials, so if you ever lose it, n8n keeps your workflows but can no longer decrypt your API keys and passwords.

File 2: docker-compose.yml

services: n8n: image: docker.n8n.io/n8nio/n8n:latest container_name: n8n restart: unless-stopped ports: - "5678:5678" environment: - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY} - GENERIC_TIMEZONE=${GENERIC_TIMEZONE} - TZ=${TZ} - N8N_HOST=localhost - N8N_PORT=5678 - N8N_PROTOCOL=http - WEBHOOK_URL=http://localhost:5678/ - N8N_RUNNERS_ENABLED=true - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true volumes: # All your data lives here: workflows, executions, credentials. # As long as this named volume is not deleted, nothing is lost. - n8n_data:/home/node/.n8n volumes: n8n_data: name: n8n_data

Start it:

docker compose up -d

The first run downloads the n8n image, which takes a minute. After that, n8n is live at http://localhost:5678 and will automatically come back after reboots.

Why this version is safe The named n8n_data volume keeps every workflow, execution, and credential outside the container. restart: unless-stopped brings n8n back after a reboot. The fixed key in .env keeps your credentials decryptable across rebuilds. No --rm, so nothing is ever auto-destroyed.
3

Create your account and open the editor

Open http://localhost:5678 in your browser. A quick note on what "localhost" means: it is simply your own computer, not a login or a session that can expire. Closing the browser tab does not touch your data. n8n keeps running in the background, and your workflows stay saved in the volume.

On the first screen, create your owner account with an email and password. You are now in the n8n editor, ready to build.

04

Build the AI agent workflow

An agent that receives a request, reasons over it with an AI model, logs it to Notion, alerts your team in Slack, and drafts a follow-up email. Add each node with the + button and connect them in order.

1

Webhook (the trigger)

Add a Webhook node. Set the HTTP Method to POST and Response Mode to Last Node. This gives you a URL that any app or form can call to kick off the agent.

IV Consulting tip Starting fresh on forms? Tally has native n8n webhook support and sets up in five minutes. Point its submission webhook at this URL.
2

AI Agent (the brain)

Add an AI Agent node and connect your Anthropic or OpenAI API key as a credential. Give it a clear system prompt that defines its job:

You are an intake assistant. Read the incoming request, classify its urgency as low, medium, or high, and write a one-paragraph summary. Be direct and specific, and never invent details that are not in the request.

The AI Agent node can reason and decide, which is what separates a real agent from a fixed rule. Claude tends to produce more natural, less generic copy, which matters when the output becomes an email.

3

Notion (the record)

Add a Notion node, set the action to create a new database entry, and map the AI summary and urgency into your chosen properties. Now every request is logged automatically, with no manual copy-pasting.

4

Slack (the alert)

Add a Slack node and send a notification to your team channel with the summary and urgency, so nothing slips through. Response time drops from hours to minutes.

5

Gmail (the follow-up)

Add a Gmail node and set the action to Create Draft, not Send. This is deliberate. You want a human to glance at the AI-written reply before it goes out, especially early on. Once you trust it, you can switch to Send.

Connect the nodes in order: Webhook to AI Agent to Notion to Slack to Gmail. Save the workflow.

6

Test before you go live

Click Test Workflow and send a sample request to your webhook URL. Watch each node light up green as it runs, and open each one to confirm the data passed through correctly. Fix any node showing red before you activate. Only when the full chain runs clean should you toggle the workflow to Active.

Build time reality check Most people complete this workflow in 45 to 90 minutes on their first attempt. If you get stuck, n8n has excellent documentation and an active community forum.
05

The throwaway command vs the safe setup

Same tool, two very different outcomes. The only real difference is whether your data lives outside the container. Here is the throwaway command from most tutorials, side by side with the persistent setup in this guide.

Setting Throwaway command Safe setup (this guide)
Data persistenceNone, lost on stopNamed volume n8n_data keeps everything
Auto-delete (--rm)Destroys the container on stopNot used
Survives rebootNoYes, restart: unless-stopped
Credential encryption keyNot setFixed key in .env
Port56785678
Best forA 5-minute throwaway demoReal automations you keep
06

How to back up and move your n8n

Because you host it yourself, you control the backups. Two simple options.

1. Export workflows from the UI

Each workflow has a Download option that saves a .json file you can import elsewhere. This is good for moving individual automations. Credentials do not export this way, by design.

2. Snapshot the whole volume

This captures everything, credentials included. From your project folder, run:

docker run --rm -v n8n_data:/data -v "$PWD":/backup busybox \ tar czf /backup/n8n-backup.tar.gz -C /data .

Keep that archive plus your .env file together. With both, you can restore your entire n8n on any machine.

One important point about multiple computers A local Docker setup does not sync to the cloud, and logging into Docker does not upload your data anywhere. Your workflows live only on the machine that holds the n8n_data volume. To reach your automations from anywhere, run n8n on a small cloud server instead, or use a managed host.
IV Consulting take If remote access and reliability are the goal, this is worth getting right once. Our Automation stage sets up production-grade self-hosted n8n with backups, remote access, and your agents wired into your real stack, so you never lose a workflow and never babysit the server.
07

Questions people ask before they start

Will my workflows be deleted if I close the browser or restart my computer?
No. With the persistent volume in this guide, your workflows and credentials are saved to disk and survive browser closes, container restarts, and full reboots. The only thing that deletes them is removing the volume itself, for example docker compose down -v or docker volume rm n8n_data. A plain docker compose down is safe.
Why should I avoid the docker run --rm command from other tutorials?
The --rm flag destroys the container when it stops, and without a volume there is no saved data, so everything you built is lost. Always run n8n with a named volume, as shown in this guide.
Does logging into Docker back up my n8n workflows to the cloud?
No. A Docker account is only for downloading container images. Your n8n data stays on your local machine inside the volume and is never uploaded. To access it from other devices, host n8n on a cloud server.
Do I need to set an encryption key?
You should. n8n uses it to encrypt your saved credentials. Setting a fixed key in your .env file means your credentials keep working even if you recreate the container or restore from a backup. If the key changes, stored credentials can no longer be decrypted.
Can I use Claude or GPT models in the AI Agent node?
Yes. The AI Agent node connects to Anthropic or OpenAI with your own API key, so you can run the latest Claude or GPT models and pay providers directly with no markup.
Is self-hosted n8n free?
The community edition is free and open source. You only pay for the server it runs on, which can be your own machine, and for any AI model usage through your provider keys. If you want a production-grade setup with backups and remote access, book a free strategy call and we will map it with you.
Ishan Vats, Founder of IV Consulting
Who wrote this

Ishan Vats

Founder, IV Consulting · AI & automation consultant

I build production AI agents, automations, and MCP servers for growing teams. 150+ ops transformations over 10+ years. If you want this mapped to your own stack, I'll do it with you on a free call.

Book a free strategy call →

Want your automation stack built for you?

Book a free 30-minute strategy call. We will map your highest-ROI workflows and give you a build roadmap on the spot. If we are not the right team for you, we will say so and point you somewhere better.

Book a Free Strategy Call →

Free 30-minute call. Honest take, even if that means "you do not need us yet."