๐ 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
โ๏ธ How it Works
-
Docker Compose Setup
-
docker-compose.ymlruns n8n in a container. - Data is persisted in
n8n_data/so you donโt lose your account or workflows after restart.
-
-
Automatic Workflow Export
- A helper script
export-workflows.shusesdocker execto dump all workflows as JSON into theworkflows/folder. - This makes it easy to track workflows in Git.
- A helper script
-
Git Pre-Commit Hook
- Before every commit,
.git/hooks/pre-commitautomatically runs the export script and stages updated JSONs. - Result: your Git history always has the latest workflows.
- Before every commit,
No more โoh crap I forgot to export that workflow.โ
๐ Usage
- Start n8n with Docker
docker compose up -d
- Commit changes to Git
- Create or edit workflows in n8n.
- Run a commit:
git commit -m "Update workflows"
- The hook auto-exports workflows โ stages them โ includes them in your commit.
- 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
.gitignoreto keep secrets and.envfiles 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)