Skip to content

Commit 0f0ce0c

Browse files
committed
Added n8n to samples
1 parent c5e2f4f commit 0f0ce0c

File tree

7 files changed

+225
-0
lines changed

7 files changed

+225
-0
lines changed

samples/n8n/.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
POSTGRES_USER=postgres
2+
POSTGRES_PASSWORD=defang123
3+
POSTGRES_DB=n8n
4+
5+
POSTGRES_NON_ROOT_USER=postgres2
6+
POSTGRES_NON_ROOT_PASSWORD=defang12345
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/[email protected]

samples/n8n/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# n8n
2+
3+
[![1-click-deploy](https://raw.githubusercontent.com/DefangLabs/defang-assets/main/Logos/Buttons/SVG/deploy-with-defang.svg)](ASK RAPH)
4+
5+
This sample shows how to get a [n8n](https://n8n.io/) app up using PostgreSQL as a database and running with Defang. The original sample can be found [here](https://github.com/n8n-io/n8n-hosting/tree/main/docker-compose/withPostgres). The official n8n guide for setting up with PostgreSQL [here](https://docs.n8n.io/hosting/installation/server-setups/docker-compose/) does not work with Defang. To use the version compatible with Defang, please refer to this sample instead.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
**IMPORTANT:** But before you do that change the default users and passwords in the [`.env`](.env) file!
16+
17+
To run the application locally for development, use the development compose file:
18+
19+
```bash
20+
docker compose -f compose.dev.yaml up
21+
```
22+
23+
This will:
24+
25+
- Start PostgreSQL with volume persistence for local development
26+
- Expose PostgreSQL on port 5432 for direct access if needed
27+
- Start n8n on port 5678 with volume persistence
28+
- Use the `init-data.sh` script to automatically set up the non-root database user
29+
30+
You can access n8n at `http://localhost:5678` once the containers are running.
31+
32+
## Configuration
33+
34+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
35+
36+
### `POSTGRES_USER`
37+
38+
The username for your Postgres database. You need to set this before deploying for the first time.
39+
40+
### `POSTGRES_PASSWORD`
41+
42+
The password for your Postgres database. You need to set this before deploying for the first time.
43+
44+
### `POSTGRES_DB`
45+
46+
The name of your Postgres database. You need to set this before deploying for the first time.
47+
48+
### `POSTGRES_NON_ROOT_USER`
49+
50+
The non-root user for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
51+
52+
### `POSTGRES_NON_ROOT_PASSWORD`
53+
54+
The POSTGRES_NON_ROOT_USER's password for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called `init-data.sh`. You need to set this before deploying for the first time.
55+
56+
## Deployment
57+
58+
The project includes two compose files:
59+
60+
- **`compose.dev.yaml`** - For local development with volume persistence and port exposure
61+
- **`compose.yaml`** - For cloud deployment (extends `compose.dev.yaml` with cloud-specific optimizations)
62+
63+
> [!NOTE]
64+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
65+
66+
### Defang Playground
67+
68+
Deploy your application to the Defang Playground by opening up your terminal and typing:
69+
70+
```bash
71+
defang compose up
72+
```
73+
74+
This will use `compose.yaml` which extends `compose.dev.yaml` but:
75+
76+
- Removes volume mounts (not supported by Defang)
77+
- Removes PostgreSQL port exposure for security
78+
- Adds a `release` service to initialize the database with proper user permissions
79+
- Optimizes environment variables for cloud deployment
80+
81+
### BYOC (AWS)
82+
83+
If you want to deploy to your own cloud account, you can use Defang BYOC:
84+
85+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
86+
2. Run in a terminal that has access to your AWS environment variables:
87+
```bash
88+
defang --provider=aws compose up
89+
```
90+
91+
---
92+
93+
Title: n8n
94+
95+
Short Description: A n8n app running on Defang.
96+
97+
Tags: n8n, PostgreSQL, Docker
98+
99+
Languages: Docker, Shell

samples/n8n/compose.dev.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: '3.8'
2+
3+
# Defang does not support volumes, therefore these volumes are only used for local development and will be ignored in production.
4+
# Consider using s3 buckets instead: https://docs.n8n.io/hosting/scaling/external-storage/
5+
volumes:
6+
db_storage:
7+
n8n_storage:
8+
9+
services:
10+
postgres:
11+
image: postgres:16
12+
restart: always
13+
environment:
14+
- POSTGRES_USER=${POSTGRES_USER}
15+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
16+
- POSTGRES_DB=${POSTGRES_DB}
17+
- POSTGRES_NON_ROOT_USER=${POSTGRES_NON_ROOT_USER}
18+
- POSTGRES_NON_ROOT_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
19+
volumes:
20+
- db_storage:/var/lib/postgresql/data
21+
- ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
22+
healthcheck:
23+
test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
24+
interval: 5s
25+
timeout: 5s
26+
retries: 10
27+
28+
n8n:
29+
image: docker.n8n.io/n8nio/n8n
30+
restart: always
31+
environment:
32+
- DB_TYPE=postgresdb
33+
- DB_POSTGRESDB_HOST=postgres
34+
- DB_POSTGRESDB_PORT=5432
35+
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
36+
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
37+
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
38+
ports:
39+
- 5678:5678
40+
links:
41+
- postgres
42+
volumes:
43+
- n8n_storage:/home/node/.n8n
44+
depends_on:
45+
postgres:
46+
condition: service_healthy

samples/n8n/compose.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "3.8"
2+
3+
services:
4+
release:
5+
# This is a one-off job to initialize the database with a non-root user
6+
build:
7+
dockerfile: release.Dockerfile
8+
command: "/init-data.sh"
9+
environment:
10+
- POSTGRES_HOST=${POSTGRES_USER}
11+
- POSTGRES_USER=${POSTGRES_USER}
12+
# Need to set PGPASSWORD so the password can be used by psql in the script init-data.sh
13+
- PGPASSWORD=${POSTGRES_PASSWORD}
14+
- POSTGRES_DB=${POSTGRES_DB}
15+
- POSTGRES_NON_ROOT_USER=${POSTGRES_NON_ROOT_USER}
16+
- POSTGRES_NON_ROOT_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
17+
restart: "no"
18+
depends_on:
19+
- postgres
20+
21+
postgres:
22+
extends:
23+
file: compose.dev.yaml
24+
service: postgres
25+
26+
n8n:
27+
extends:
28+
file: compose.dev.yaml
29+
service: n8n
30+
depends_on:
31+
release:
32+
condition: service_completed_successfully
33+
postgres:
34+
condition: service_healthy

samples/n8n/init-data.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e;
3+
4+
5+
if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then
6+
# Need to add --host because it is run in a separate container
7+
psql -v ON_ERROR_STOP=1 --host="$POSTGRES_HOST" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
8+
CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}';
9+
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER};
10+
GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER};
11+
EOSQL
12+
else
13+
echo "SETUP INFO: No Environment variables given!"
14+
fi

samples/n8n/release.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM postgres:16
2+
3+
COPY init-data.sh /
4+
5+
ENTRYPOINT ["sh", "-c"]

0 commit comments

Comments
 (0)