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)