n8n Docker Compose A to Z Guidance

 


Here's a Docker Compose configuration for n8n, the workflow automation tool, including basic setup with PostgreSQL for persistence:


```yaml

version: '3'


services:

  n8n:

    image: n8nio/n8n

    container_name: n8n

    restart: unless-stopped

    ports:

      - "5678:5678"

    environment:

      - N8N_BASIC_AUTH_ACTIVE=true

      - N8N_BASIC_AUTH_USER=<your-username>

      - N8N_BASIC_AUTH_PASSWORD=<your-password>

      - DB_TYPE=postgresdb

      - DB_POSTGRESDB_DATABASE=n8n

      - DB_POSTGRESDB_HOST=postgres

      - DB_POSTGRESDB_PORT=5432

      - DB_POSTGRESDB_USER=n8n

      - DB_POSTGRESDB_PASSWORD=n8n

      - N8N_HOST=${HOSTNAME}

    volumes:

      - n8n_data:/home/node/.n8n

    depends_on:

      - postgres

    networks:

      - n8n_network


  postgres:

    image: postgres:14

    container_name: postgres

    restart: unless-stopped

    environment:

      - POSTGRES_DB=n8n

      - POSTGRES_USER=n8n

      - POSTGRES_PASSWORD=n8n

    volumes:

      - postgres_data:/var/lib/postgresql/data

    networks:

      - n8n_network


volumes:

  n8n_data:

  postgres_data:


networks:

  n8n_network:

    driver: bridge

```


---


Key Configuration Notes

1. Authentication:

   - Update `<your-username>` and `<your-password>` for basic auth

   - Remove `N8N_BASIC_AUTH_*` env vars if you want open access (not recommended)


2. Database:

   - PostgreSQL is configured for data persistence

   - Change database credentials in both n8n and postgres services


3. Ports

   - Web UI accessible at `http://localhost:5678`

   - Adjust host port (`5678`) if needed


4. Volumes

   - Persistent storage for workflows and PostgreSQL data

   - Data survives container restarts/updates

How to Use

1. Save as `docker-compose.yml`

2. Run:

   ```bash

   docker-compose up -d

   ```

3. Access the UI at `http://localhost:5678`


---


Common Customizations

Add Email/SMTP:

```yaml

environment:

  - N8N_EMAIL_MODE=internal

  - N8N_SMTP_HOST=smtp.example.com

  - N8N_SMTP_PORT=587

  - N8N_SMTP_USER=user@example.com

  - N8N_SMTP_PASSWORD=your-password

```


Enable Webhook URLs

```yaml

environment:

  - WEBHOOK_URL=https://your-domain.com

```


Scale Workers

```yaml

environment:

  - N8N_EXECUTIONS_MODE=queue

  - N8N_QUEUE_BULL_REDIS_HOST=redis

  - N8N_QUEUE_BULL_REDIS_DB=0

```

Troubleshooting

1. Check logs

   ```bash

   docker-compose logs -f n8n

   ```

2. Verify database connection

   ```bash

   docker exec -it postgres psql -U n8n -d n8n

   ```

3. Reset credentials

   ```bash

   docker-compose down -v && docker-compose up -d

   ```


For production deployments, consider adding:

- Reverse proxy (Nginx/Caddy)

- SSL certificates

- Redis for queue management

- Backup strategy for volumes


Previous Post Next Post