Home
Softono
n8n-setup-docker

n8n-setup-docker

Open source MIT Shell
13
Stars
3
Forks
0
Issues
1
Watchers
1 year
Last Commit

About n8n-setup-docker

Fast, safe and smart setup for self-hosted n8n placed in a Docker container, ready to host and connect to postgresql database.

Platforms

Web Self-hosted Docker

Languages

Shell

Links

#   ▄▄        ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄        ▄             ▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄    ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄             ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄         ▄ ▄▄▄▄▄▄▄▄▄▄▄ 
#  ▐░░▌      ▐░▐░░░░░░░░░░░▐░░▌      ▐░▌           ▐░░░░░░░░░░▌▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌  ▐░▐░░░░░░░░░░░▐░░░░░░░░░░░▌           ▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌       ▐░▐░░░░░░░░░░░▌
#  ▐░▌░▌     ▐░▐░█▀▀▀▀▀▀▀█░▐░▌░▌     ▐░▌           ▐░█▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀▀▀▐░▌ ▐░▌▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀▀▀▀█░▌           ▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀▐░▌       ▐░▐░█▀▀▀▀▀▀▀█░▌
#  ▐░▌▐░▌    ▐░▐░▌       ▐░▐░▌▐░▌    ▐░▌           ▐░▌       ▐░▐░▌       ▐░▐░▌         ▐░▌▐░▌ ▐░▌         ▐░▌       ▐░▌           ▐░▌         ▐░▌              ▐░▌    ▐░▌       ▐░▐░▌       ▐░▌
#  ▐░▌ ▐░▌   ▐░▐░█▄▄▄▄▄▄▄█░▐░▌ ▐░▌   ▐░▌▄▄▄▄▄▄▄▄▄▄▄▐░▌       ▐░▐░▌       ▐░▐░▌         ▐░▌░▌  ▐░█▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄█░▌▄▄▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄▄▄     ▐░▌    ▐░▌       ▐░▐░█▄▄▄▄▄▄▄█░▌
#  ▐░▌  ▐░▌  ▐░▌▐░░░░░░░░░▌▐░▌  ▐░▌  ▐░▐░░░░░░░░░░░▐░▌       ▐░▐░▌       ▐░▐░▌         ▐░░▌   ▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▌    ▐░▌    ▐░▌       ▐░▐░░░░░░░░░░░▌
#  ▐░▌   ▐░▌ ▐░▐░█▀▀▀▀▀▀▀█░▐░▌   ▐░▌ ▐░▌▀▀▀▀▀▀▀▀▀▀▀▐░▌       ▐░▐░▌       ▐░▐░▌         ▐░▌░▌  ▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀█░█▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀▀▀     ▐░▌    ▐░▌       ▐░▐░█▀▀▀▀▀▀▀▀▀ 
#  ▐░▌    ▐░▌▐░▐░▌       ▐░▐░▌    ▐░▌▐░▌           ▐░▌       ▐░▐░▌       ▐░▐░▌         ▐░▌▐░▌ ▐░▌         ▐░▌     ▐░▌                       ▐░▐░▌              ▐░▌    ▐░▌       ▐░▐░▌          
#  ▐░▌     ▐░▐░▐░█▄▄▄▄▄▄▄█░▐░▌     ▐░▐░▌           ▐░█▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄▄▄▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄▐░▌      ▐░▌             ▄▄▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄▄▄     ▐░▌    ▐░█▄▄▄▄▄▄▄█░▐░▌          
#  ▐░▌      ▐░░▐░░░░░░░░░░░▐░▌      ▐░░▌           ▐░░░░░░░░░░▌▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌  ▐░▐░░░░░░░░░░░▐░▌       ▐░▌           ▐░░░░░░░░░░░▐░░░░░░░░░░░▌    ▐░▌    ▐░░░░░░░░░░░▐░▌          
#   ▀        ▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀        ▀▀             ▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀    ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀         ▀             ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀      ▀      ▀▀▀▀▀▀▀▀▀▀▀ ▀           
#                                                                                                                                                                                              
🌀 Seamless DevOps. Powered by Swarm, Secured by Secrets.

n8n docker secure built-by


🔧 n8n-setup-docker: Secure, Custom, Ready for Action

This repo delivers a production-friendly n8n stack using:

  • 🐳 Docker Swarm for container orchestration
  • 🔐 Docker Secrets for all sensitive env vars
  • ⚙️ Custom scripts to prep, deploy, and launch cleanly
  • 🧠 Readable, modifiable, highly hackable code

If you want a secure, local-AND-remote deployable n8n instance with PostgreSQL and zero manual docker CLI poking…
This is it.


🧠 Project Tree

./
├── docker-compose.yaml              # Swarm service definition
├── Dockerfile                       # Custom n8n image with psql & logic
├── container_run.sh                 # Entry script inside container
├── docker_swarm_deploy.sh           # Dev’s best friend — deploys everything
├── docker_clear_env_vars.sh         # Script to remove or unset sensitive environment variables from the shell/session.
├── docker_destroy_container.sh      # Script to fully stop and remove Docker stack, containers, volumes, and leave Swarm.
├── README.md                        # Project overview, usage instructions, diagrams, and badges. Your main documentation.
├── Docker_cheatsheet.md             # Handy reference for common Docker commands tailored to your workflow.
├── .gitignore                       # Git config file to exclude specified files/folders (like secrets, volumes, etc.) from version control.
├── .dockerignore                    # Docker config file to exclude files/folders from being copied into Docker build context.
├── secrets/                         # 🔥 Sensitive data here (not git-tracked!)
│   ├── N8N_BASIC_AUTH_USER.txt
│   ├── N8N_BASIC_AUTH_PASSWORD.txt
│   ├── N8N_ENCRYPTION_KEY.txt
│   ├── DB_POSTGRESDB_HOST.txt
│   ├── DB_POSTGRESDB_PORT.txt
│   ├── DB_POSTGRESDB_DATABASE.txt
│   ├── DB_POSTGRESDB_SCHEMA.txt
│   ├── DB_POSTGRESDB_USER.txt
│   ├── DB_POSTGRESDB_PASSWORD.txt
│   └── N8N_PORT.txt                  # optional

🚀 Quickstart

1️⃣ Initialize Docker Swarm (if needed)

# only needed on the first run:
docker swarm init

2️⃣ Add Your Secrets

Drop plain-text secret files into the ./secrets/ in the project dir. Example:


echo "admin"    > secrets/N8N_BASIC_AUTH_USER.txt
echo "hunter2"  > secrets/N8N_BASIC_AUTH_PASSWORD.txt
echo "myKeyBase" > secrets/N8N_ENCRYPTION_KEY.txt

echo "db.example.com" > secrets/DB_POSTGRESDB_HOST.txt
echo "public"         > secrets/DB_POSTGRESDB_SCHEMA.txt
echo "5432"           > secrets/DB_POSTGRESDB_PORT.txt
echo "n8n_user"       > secrets/DB_POSTGRESDB_USER.txt
echo "pa$$w0rd"       > secrets/DB_POSTGRESDB_PASSWORD.txt
echo "n8n"            > secrets/DB_POSTGRESDB_DATABASE.txt

And the same for your DB creds.


3️⃣ Deploy It Like a Boss

./docker_swarm_deploy.sh

This will:

🏗️ Build your my-n8n:latest image

🔐 Create (or update) all Docker secrets

📦 Deploy a Swarm stack (n8n_stack) with those secrets

🧠 Auto-detect your timezone and inject it too!

4️⃣ Check Docker logs:

docker service logs -f n8n_stack_n8n

And that's it - n8n is set up and ready! In case of online deployment you'll be able to access your automations from anywhere! (login using the values from your secret files)


🔍 What's Actually Happening?

Your container launches via: container_run.sh:

🧪 Loads secrets from /run/secrets/…

🔁 Converts them into environment variables

✅ Tests PostgreSQL DB connection via psql

🎬 Runs n8n ONLY if everything is healthy

💡 Fail fast, fail clean — or don’t fail at all.


♻️ Rotating Secrets

Secrets are immutable in Docker Swarm. To update:

# Edit files in ./secrets/*.txt

# Then:
./docker_swarm_deploy.sh

This deletes+recreates them automatically.


💾 Helpful scripts

(mostly helpful during development, not recommended for production):

💥 Getting rid of Docker Containers

Wanna burn it down?

./docker_destroy_container.sh

(Watch out - the script stops and removes all the Docker containers!)


🧼 Cleaning Up env variables

In case mess in env variables is giving you a headache:

./docker_clear_env_variables.sh

👽 Need to reset n8n credentials within Docker container?

# This will list running containers, localise id or name of running container with n8n:
docker ps

# Execute n8n credentials reset command inside container:
docker exec -it <container_name_or_id> n8n user-management:reset

📘 Docker Cheatsheet

📎 Check out the Docker_cheatsheet.md for more handy commands!

(I see you, automation fiend 😎)


💪 Powered By

🧠 Platform 🌐 Link
🔧 n8n – powerful workflow automation 🧩 Docs
🐳 Docker – containerize all the things 📦 Hub
🐘 PostgreSQL – battle-tested relational database 📚 Docs
🤖 ChatGPT – natural language assistant magic ✨ 🔍 OpenAI

🚀 Free Tier PostgreSQL Hosting for n8n Self-Hosting (examples):

🏢 Service 💸 Free Tier Highlights 🔗 Link
🦄 Supabase – Open-source Firebase alternative with Postgres backend, perfect for real-time & n8n integration 🆓 500 MB storage, 2 million requests/month, 24/7 uptime, generous free tier 🔥 Supabase Free
☁️ ElephantSQL – Simple managed PostgreSQL hosting with free shared plans 🆓 20 MB storage, easy to connect, ideal for dev & small projects 🐘 ElephantSQL Plans
☁️ Heroku Postgres – Classic cloud PaaS with free tier database 🆓 1,000 rows, 20 connections, 10k rows/day writes, sleeps after inactivity (may impact response time) 🚀 Heroku Postgres Free
☁️ Neon – Serverless Postgres with automatic scaling and free tier 🆓 10 GB storage, 1 million monthly requests, modern cloud-native Postgres ⚡️ Neon Free Tier
🐦 Aiven – Fully managed PostgreSQL with free trial and credits 🎁 30-day free trial with $30 credit, easy migration & high availability 🎉 Aiven Free Trial
☁️ Citus Data on Azure – Horizontal scale-out Postgres, free for dev/test 🆓 $200 Azure credit for 30 days + free tier options 💎 Azure PostgreSQL

State of affairs as for June 2025


☁️ Awesome Free (or Generous Trial) Shell Hosting for Remote Docker Swarm & n8n Frontend

🏢 Service 💸 Free Tier / Trial Highlights 🔗 Link
🚀 Google Cloud Shell – Fully featured shell environment with 5 GB persistent storage 🆓 Always free, built-in Docker support, easy to connect via browser, perfect for lightweight dev & Docker experiments 🌐 Google Cloud Shell
🐳 Railway – Developer-friendly cloud with free tier for containers and shell access 🆓 $5 monthly credit (~500 hours), easy deployment, Docker support, persistent volumes 🎉 Railway Free Tier
☁️ Fly.io – Global app hosting with Docker container support, 3 shared CPUs 🆓 3 shared CPUs, 256 MB RAM, 3 GB persistent volume, free 1600 CPU hours/month ✈️ Fly.io Free Tier
🐧 Oracle Cloud Free Tier – Always free compute instances with shell access 🆓 2 AMD VMs with 1/8 OCPU each, 1 GB RAM, Docker installable, 100 GB block storage 🐘 Oracle Cloud Free Tier
🦄 AWS Free Tier (EC2) – Free micro instance for 12 months with full shell access 🆓 750 hrs/month t2.micro/t3.micro, Docker installable, perfect for small dev projects ☁️ AWS Free Tier
🐳 DigitalOcean App Platform – Easy Docker container deploy with free starter plan 🆓 Starter tier with 3 static sites, 1 container, 100 GB bandwidth 🚀 DigitalOcean App Platform

State of affairs as for June 2025


🐵 Development plans:

  • Kubernetes orchestration of containers with various customizable settings.
  • Increase of installation options of popular services n8n connects to on setup.
  • A script to dump workflows and creds from n8n regularly (via API or CLI). Re-import them with new keys and never lose your data.
  • Much more to come...

© 2025 Paweł Kochanowicz · GitHub

This project is licensed under the MIT License.