Deploy n8n with Docker Compose
n8n is a powerful workflow automation tool that allows you to connect different services and automate tasks. While n8n offers a cloud version, self-hosting provides more control and flexibility. The recommended way to self-host n8n is by using Docker. In this guide, we will walk through how to deploy n8n using Docker Compose, which simplifies the management of multi-container Docker applications.
Quick Glance: docker-compose.yml for n8n
For those who want to get straight to it, here is a docker-compose.yml file for a basic n8n setup with a PostgreSQL database.
version: '3.8'
services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER=${N8N_BASIC_AUTH_USER}
      - N8N_BASIC_AUTH_PASSWORD=${N8N_BASIC_AUTH_PASSWORD}
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - postgres
  postgres:
    image: postgres:13
    restart: always
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
volumes:
  n8n_data:
  postgres_data:
You will also need a .env file in the same directory to store your credentials:
# PostgreSQL
POSTGRES_DB=n8n
POSTGRES_USER=n8nuser
POSTGRES_PASSWORD=yoursecurepassword
# n8n Basic Auth
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=yoursecureadminpassword
# Timezone
GENERIC_TIMEZONE=Asia/Shanghai
Prerequisites
Before you start, make sure you have Docker and Docker Compose installed on your system.
- Docker Desktop (includes Docker Compose)
- Docker Engine and Docker Compose for Linux servers.
Deploying n8n
- Create a directory for your n8n setup and navigate into it. - mkdir n8n-deployment cd n8n-deployment
- Create the - docker-compose.ymlfile and paste the content from the "Quick Glance" section above.
- Create the - .envfile and add your desired credentials.
- Start the services using Docker Compose. - bash docker compose up -dThis command will pull the necessary Docker images and start the n8n and PostgreSQL containers in the background.
Accessing n8n
Once the containers are running, you can access your n8n instance by navigating to http://localhost:5678 in your web browser. You will be prompted to set up an owner account for your n8n instance.
Updating n8n
To update your n8n instance to the latest version, follow these steps:
- Pull the latest n8n image: - docker compose pull n8n
- Restart the services: - bash docker compose up -dDocker Compose will recreate the n8n container with the new image while keeping your data stored in the Docker volumes.
By using Docker Compose, you can easily deploy, manage, and update your self-hosted n8n instance. This setup provides a robust and scalable environment for your workflow automations.
