An open API service indexing awesome lists of open source software.

https://github.com/pkochanowicz/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.
https://github.com/pkochanowicz/n8n-setup-docker

automation bash chatgpt devops docker n8n open-source postgresql self-hosted shell stable-diffusion workflow-automation

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

          

```
# ▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄ ▄ ▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄ ▄ ▄▄▄▄▄▄▄▄▄▄▄
# ▐░░▌ ▐░▐░░░░░░░░░░░▐░░▌ ▐░▌ ▐░░░░░░░░░░▌▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌ ▐░▐░░░░░░░░░░░▐░░░░░░░░░░░▌ ▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌ ▐░▐░░░░░░░░░░░▌
# ▐░▌░▌ ▐░▐░█▀▀▀▀▀▀▀█░▐░▌░▌ ▐░▌ ▐░█▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀▀▀▐░▌ ▐░▌▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀▀▀▀█░▌ ▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀▐░▌ ▐░▐░█▀▀▀▀▀▀▀█░▌
# ▐░▌▐░▌ ▐░▐░▌ ▐░▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▐░▌ ▐░▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▐░▌ ▐░▌
# ▐░▌ ▐░▌ ▐░▐░█▄▄▄▄▄▄▄█░▐░▌ ▐░▌ ▐░▌▄▄▄▄▄▄▄▄▄▄▄▐░▌ ▐░▐░▌ ▐░▐░▌ ▐░▌░▌ ▐░█▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄█░▌▄▄▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄▄▄▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌ ▐░▐░█▄▄▄▄▄▄▄█░▌
# ▐░▌ ▐░▌ ▐░▌▐░░░░░░░░░▌▐░▌ ▐░▌ ▐░▐░░░░░░░░░░░▐░▌ ▐░▐░▌ ▐░▐░▌ ▐░░▌ ▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▐░░░░░░░░░░░▌ ▐░▌ ▐░▌ ▐░▐░░░░░░░░░░░▌
# ▐░▌ ▐░▌ ▐░▐░█▀▀▀▀▀▀▀█░▐░▌ ▐░▌ ▐░▌▀▀▀▀▀▀▀▀▀▀▀▐░▌ ▐░▐░▌ ▐░▐░▌ ▐░▌░▌ ▐░█▀▀▀▀▀▀▀▀▀▐░█▀▀▀▀█░█▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀█░▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ ▐░▌ ▐░▐░█▀▀▀▀▀▀▀▀▀
# ▐░▌ ▐░▌▐░▐░▌ ▐░▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▐░▌ ▐░▐░▌ ▐░▌▐░▌ ▐░▌ ▐░▌ ▐░▌ ▐░▐░▌ ▐░▌ ▐░▌ ▐░▐░▌
# ▐░▌ ▐░▐░▐░█▄▄▄▄▄▄▄█░▐░▌ ▐░▐░▌ ▐░█▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄▄▄▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄▐░▌ ▐░▌ ▄▄▄▄▄▄▄▄▄█░▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▐░▌
# ▐░▌ ▐░░▐░░░░░░░░░░░▐░▌ ▐░░▌ ▐░░░░░░░░░░▌▐░░░░░░░░░░░▐░░░░░░░░░░░▐░▌ ▐░▐░░░░░░░░░░░▐░▌ ▐░▌ ▐░░░░░░░░░░░▐░░░░░░░░░░░▌ ▐░▌ ▐░░░░░░░░░░░▐░▌
# ▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀
#
🌀 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

```txt
./
├── 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)

```bash
# 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:

```bash

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

```bash
./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:

```bash
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:

```bash
# 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?

```bash
./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:

```bash
./docker_clear_env_variables.sh
```

---

#### 👽 Need to reset n8n credentials within Docker container?

```bash
# 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 n8n user-management:reset
```

---

### 📘 Docker Cheatsheet
📎 Check out the [Docker_cheatsheet.md](./Docker_cheatsheet.md) for more handy commands!

#### (I see you, automation fiend 😎)

---

## 💪 Powered By

| 🧠 Platform| 🌐 Link |
|------------|---------|
| 🔧 **[n8n](https://n8n.io/)** – powerful workflow automation | 🧩 [Docs](https://docs.n8n.io/) |
| 🐳 **[Docker](https://www.docker.com/)** – containerize all the things | 📦 [Hub](https://hub.docker.com/) |
| 🐘 **[PostgreSQL](https://www.postgresql.org/)** – battle-tested relational database | 📚 [Docs](https://www.postgresql.org/docs/) |
| 🤖 **[ChatGPT](https://chat.openai.com/)** – natural language assistant magic ✨ | 🔍 [OpenAI](https://openai.com/chatgpt) |

---

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

| 🏢 Service | 💸 Free Tier Highlights | 🔗 Link |
|-----------|-------------------------|---------|
| 🦄 **[Supabase](https://supabase.com/)** – 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](https://supabase.com/pricing) |
| ☁️ **[ElephantSQL](https://www.elephantsql.com/)** – Simple managed PostgreSQL hosting with free shared plans | 🆓 20 MB storage, easy to connect, ideal for dev & small projects | 🐘 [ElephantSQL Plans](https://www.elephantsql.com/plans.html) |
| ☁️ **[Heroku Postgres](https://www.heroku.com/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](https://www.heroku.com/pricing#postgres) |
| ☁️ **[Neon](https://neon.tech/)** – Serverless Postgres with automatic scaling and free tier | 🆓 10 GB storage, 1 million monthly requests, modern cloud-native Postgres | ⚡️ [Neon Free Tier](https://neon.tech/pricing) |
| 🐦 **[Aiven](https://aiven.io/postgresql)** – Fully managed PostgreSQL with free trial and credits | 🎁 30-day free trial with $30 credit, easy migration & high availability | 🎉 [Aiven Free Trial](https://aiven.io/pricing) |
| ☁️ **[Citus Data on Azure](https://azure.microsoft.com/en-us/services/postgresql/)** – Horizontal scale-out Postgres, free for dev/test | 🆓 $200 Azure credit for 30 days + free tier options | 💎 [Azure PostgreSQL](https://azure.microsoft.com/en-us/pricing/details/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](https://cloud.google.com/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](https://cloud.google.com/shell) |
| 🐳 **[Railway](https://railway.app/)** – 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](https://railway.app/pricing) |
| ☁️ **[Fly.io](https://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](https://fly.io/docs/about/pricing/) |
| 🐧 **[Oracle Cloud Free Tier](https://www.oracle.com/cloud/free/)** – 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](https://www.oracle.com/cloud/free/) |
| 🦄 **[AWS Free Tier (EC2)](https://aws.amazon.com/free/)** – 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](https://aws.amazon.com/free/) |
| 🐳 **[DigitalOcean App Platform](https://www.digitalocean.com/products/app-platform/)** – Easy Docker container deploy with free starter plan | 🆓 Starter tier with 3 static sites, 1 container, 100 GB bandwidth | 🚀 [DigitalOcean App Platform](https://www.digitalocean.com/pricing/) |

*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](https://github.com/pkochanowicz)

This project is licensed under the [MIT License](LICENSE).