Introduction
Welcome to the next major phase of our course. Having mastered the art of building complex and resilient CI/CD workflows, we now turn our attention to deploying and managing the n8n platform itself. To run your automation pipelines in a real-world, production scenario, you need a robust, scalable, and configurable environment. This lesson marks the first step in building that environment.
Leveraging your high proficiency with Docker, we will deploy a self-hosted n8n instance using Docker Compose. This method provides the control and flexibility necessary for production use. We will focus on creating a configurable setup by externalizing settings into environment variables, a best practice you are likely familiar with from other CI/CD and software development contexts.
By the end of this lesson, you will be able to deploy a fresh n8n instance, configured via environment variables and orchestrated by Docker Compose, laying the foundation for a production-grade automation platform.
The Self-Hosted Architecture
Before we begin writing files, it's essential to understand the architecture we are building. The most common and recommended pattern for a production-ready, self-hosted n8n instance involves several distinct components working together, all managed by Docker.
For this initial deployment, our architecture will consist of:
- An n8n container, which runs the main application, editor, and workflow execution engine.
- A PostgreSQL container, which will serve as the persistent database for our workflows, credentials, and execution logs. While n8n can use a simple SQLite database by default, using a dedicated database like PostgreSQL is crucial for performance and scalability in production.
- Docker named volumes to ensure that our n8n configuration and database data persist even if the containers are removed or re-created.
The guide "Self-Host n8n with Docker on a VPS" provides a clear overview of this setup.
Self-Host n8n with Docker on a VPS
This guide from GravityWP provides an excellent, practical walkthrough of the deployment pattern we'll be using. It aligns perfectly with production best practices.
First, read the introduction to understand the "full control" approach we're taking. Review the section What this setup looks like. It outlines the core components you'll be managing. Finally, study the High-level architecture section and its diagram. This will give you a clear mental model of how the pieces connect. Note that we will address the "Reverse proxy" component in a future lesson; for now, we'll focus on the n8n and Postgres containers.
Configuration with Environment Variables: The .env File
A key principle of a robust deployment is the separation of configuration from the application's orchestration. We will achieve this by using a .env file to store all our settings, such as URLs, encryption keys, and database credentials. Docker Compose will then read this file to configure our containers at startup.
This approach keeps your docker-compose.yml file clean and free of sensitive information, making it safe to commit to version control.
The video from Techno Tim provides a clear walkthrough of the essential variables you'll need to define.
Self-Host Your Own Automation Platform with n8n + Docker
This video by Techno Tim, "Self-Host Your Own Automation Platform with n8n + Docker," gives a great explanation of the key environment variables you'll need for a self-hosted setup.
Watch the segment from creating a .env file. Pay close attention to his explanation of these critical variables: GENERIC_TIMEZONE: For scheduling. WEBHOOK_URL: Crucial for n8n to generate correct webhook URLs. NODE_ENV=production: An important optimization for the underlying Node.js runtime.
Based on that, here is a template for your .env file. You will need to create this file in your project directory.
# Basic
N8N_HOST=n8n.yourdomain.com
WEBHOOK_URL=https://n8n.yourdomain.com/
N8N_PORT=5678
GENERIC_TIMEZONE=Europe/Amsterdam # Change to your timezone
TZ=Europe/Amsterdam # Change to your timezone
# Encryption - VERY IMPORTANT
# Generate a key with: openssl rand -hex 32
N8N_ENCRYPTION_KEY=your-long-random-32-byte-hex-string-here
# Database (Postgres)
DB_TYPE=postgresdb
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_HOST=postgres
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_USER=n8n
DB_POSTGRESDB_PASSWORD=change_this_to_a_strong_password
DB_POSTGRESDB_SCHEMA=public
A critical note on N8N_ENCRYPTION_KEY: This key is used to encrypt all credentials stored in your n8n instance. You must generate a strong, random key and keep it safe. If you lose this key, you will lose access to all your saved credentials. You can generate a suitable key by running openssl rand -hex 32 in your terminal.
Defining the Stack: The docker-compose.yml File
Now, let's define the services themselves in a docker-compose.yml file. Given your Docker expertise, the structure will be very familiar. We will define two services: postgres and n8n.
The following image shows how the .env file and docker-compose.yml will look, working together to configure the stack.

Let's examine the docker-compose.yml file you will create. This example is adapted from the GravityWP guide and is a perfect minimal, production-oriented starting point.
version: "3.8"
services:
postgres:
image: postgres:15
restart: unless-stopped
environment:
POSTGRES_DB: ${DB_POSTGRESDB_DATABASE}
POSTGRES_USER: ${DB_POSTGRESDB_USER}
POSTGRES_PASSWORD: ${DB_POSTGRESDB_PASSWORD}
volumes:
- n8n_db_data:/var/lib/postgresql/data
n8n:
image: docker.n8n.io/n8nio/n8n
restart: unless-stopped
env_file:
- .env
ports:
- "127.0.0.1:5678:${N8N_PORT}"
depends_on:
- postgres
volumes:
- n8n_data:/home/node/.n8n
volumes:
n8n_data:
external: false
n8n_db_data:
external: false
Let's quickly review the key elements:
services: Defines our two containers,postgresandn8n.environment(inpostgresservice): This shows how Docker Compose substitutes variables (e.g.,${DB_POSTGRESDB_DATABASE}) from the environment to configure the PostgreSQL container.env_file(inn8nservice): This directive tells Docker Compose to load all variables from our.envfile and make them available inside then8ncontainer.volumes: We define two named volumes,n8n_dataandn8n_db_data. This is preferable to bind mounts for application data as it abstracts away the host's filesystem structure and is managed by Docker, simplifying backups and migration.depends_on: Ensures thepostgrescontainer is started before then8ncontainer, which is a necessary startup dependency.
Deployment and Verification
With the configuration files ready, you can now deploy the stack.
Self-Host n8n with Docker on a VPS
This guide provides the final sequence of commands to get your instance up and running.
Follow the hands-on steps described in these sections: Create the project directory and your .env file as we've discussed. Create the Docker volumes ahead of time. While Docker Compose can create them, doing it manually ensures ownership is correct. Run docker volume create n8n_data and docker volume create n8n_db_data. Write the docker-compose.yml file as shown above. Finally, start the stack and test it. Use docker compose up -d to launch the containers. After running docker compose up -d, you can verify that both containers are running with docker ps. You should see n8n and postgres containers in the list. To troubleshoot, you can inspect the logs of either container using docker logs <container_name>. Once running, you can access your new n8n instance by navigating to http://localhost:5678 in your browser. You will be prompted to create your owner account, which finalizes the setup.
Conclusion
In this lesson, you have successfully deployed a configurable, self-hosted n8n instance using Docker Compose. You have applied your existing Docker knowledge to a new application, establishing a solid foundation for a production environment.
Key Takeaways:
- Docker Compose is the standard for deploying self-hosted n8n instances, allowing you to define and manage multi-container applications.
- Environment variables (
.envfile) are the best practice for separating configuration from orchestration, keeping your setup clean and your secrets safe. - A production-ready setup for n8n should use a robust database backend like PostgreSQL from the start.
- Named volumes are essential for ensuring the persistence of your n8n configuration, workflows, and database across container restarts and upgrades.
Now that your instance is running with a PostgreSQL container, our next lesson will delve deeper into the database itself. We will discuss why PostgreSQL is the preferred choice, how to manage it, and explore more advanced configuration options to ensure your n8n installation is truly production-grade.