DEV Community

Cover image for n8n self-hosting with persistent workflows
Oussama Belhadi
Oussama Belhadi

Posted on

n8n self-hosting with persistent workflows

🚀 How I Self-Host n8n with Docker + Git for Persistent Workflows

If you’re like me and love automating things with n8n, self-hosting is the best way to have full control over your workflows.

But there’s a catch:

👉 By default, workflows are stored inside the container’s DB. If something breaks, you risk losing them.

👉 And moving workflows between devices isn’t exactly plug & play.

So, I built a simple setup that makes self-hosting persistent, Git-friendly, and portable, using Docker + Git hooks.

You can find the repo here


📦 Project Structure

Here’s the layout of my n8n-docker repo:

n8n-docker/
├── docker-compose.yml      # Docker Compose setup for n8n
├── export-workflows.sh     # Script to export all workflows as JSON
├── workflows/              # Folder where exported workflows are stored (Git-tracked)
├── n8n\_data/               # Docker volume for SQLite DB, credentials, and user account
└── .git/hooks/
└── pre-commit          # Git hook to auto-export workflows before commit
Enter fullscreen mode Exit fullscreen mode

⚙️ How it Works

  1. Docker Compose Setup

    • docker-compose.yml runs n8n in a container.
    • Data is persisted in n8n_data/ so you don’t lose your account or workflows after restart.
  2. Automatic Workflow Export

    • A helper script export-workflows.sh uses docker exec to dump all workflows as JSON into the workflows/ folder.
    • This makes it easy to track workflows in Git.
  3. Git Pre-Commit Hook

    • Before every commit, .git/hooks/pre-commit automatically runs the export script and stages updated JSONs.
    • Result: your Git history always has the latest workflows.

No more “oh crap I forgot to export that workflow.”


🚀 Usage

  1. Start n8n with Docker
docker compose up -d
Enter fullscreen mode Exit fullscreen mode
  1. Commit changes to Git
  • Create or edit workflows in n8n.
  • Run a commit:
git commit -m "Update workflows"
Enter fullscreen mode Exit fullscreen mode
  • The hook auto-exports workflows → stages them → includes them in your commit.
  1. Move to another device
  • Clone the repo.
  • Run docker compose up -d.
  • Import workflows from the workflows/ folder.
  • (Note: credentials are not included — you’ll need to recreate those manually).

⚠️ Notes

  • The container must be running for workflow export to work.
  • Credentials are stored encrypted in n8n_data/, not exported to Git.
  • Add a .gitignore to keep secrets and .env files safe.

đź”— References


This setup has been a game-changer for me — it feels like using n8n with built-in Git support.
Now, even if I nuke my server, I can recover my workflows in minutes.

Top comments (0)