n8n Masterclass IModule 1

1.2Docker Setup & Deployment

30 min 6 code blocks Practice Lab Quiz (5Q)

Docker Setup and Deployment: Creating the Factory Ground

Docker is the foundation of the Automated Growth Empire. It allows you to run complex software like n8n or PostgreSQL in isolated "containers," ensuring your infrastructure is identical across your local laptop, a Karachi co-working space WiFi, and a production VPS in Germany. Once you understand Docker, deploying n8n anywhere takes under 10 minutes.

The Docker Mental Model

Before touching any commands, understand the four core Docker concepts. Everything else builds on these.

TermConceptIndustrial Analogy
ImageThe blueprint of the software — a read-only templateThe factory schematic (a PDF of the design)
ContainerA running instance of an image — isolated and ephemeralThe active factory floor (can be stopped, restarted)
VolumePersistent storage that survives container restartsThe warehouse — goods stay even if the floor shuts down
NetworkHow containers communicate with each otherThe internal phone and conveyor belt system
docker-composeA YAML file that defines multi-container setupsThe master project plan that coordinates all departments

The Docker Architecture for n8n

code
Your VPS or Local Machine
├── Docker Engine (Container Runtime)
│   ├── n8n Container (Port 5678)
│   │   ├── n8n application process
│   │   ├── Embedded SQLite database (default)
│   │   └── Workflow execution engine
│   │
│   ├── Volume: n8n_data
│   │   ├── /home/node/.n8n/config (settings)
│   │   ├── /home/node/.n8n/database.sqlite (all workflows + credentials)
│   │   └── /home/node/.n8n/nodes (custom nodes if installed)
│   │
│   └── Network: n8n_default
│       └── Internal DNS: n8n → 172.18.0.2
│
└── Host ports exposed: 5678 → container:5678

The volume is the critical piece. Without it, stopping your container destroys all your workflows. With a named volume, your data persists through restarts, upgrades, and crashes.

The docker-compose.yml for n8n

This is the production-standard file. Save it as docker-compose.yml in a directory like /opt/n8n/ on your VPS:

yaml
version: '3.8'
services:
  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_HOST=automate.yourdomain.com
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://automate.yourdomain.com/
      - GENERIC_TIMEZONE=Asia/Karachi
      - TZ=Asia/Karachi
    volumes:
      - n8n_data:/home/node/.n8n
    networks:
      - n8n_network

volumes:
  n8n_data:
    driver: local

networks:
  n8n_network:
    driver: bridge

Key fields to understand:

  • restart: always — container auto-starts on server reboot (critical for 24/7 uptime)
  • GENERIC_TIMEZONE=Asia/Karachi — all scheduled triggers fire in PKT (UTC+5), not UTC
  • WEBHOOK_URL — tells n8n what its public URL is so webhook URLs are generated correctly
  • n8n_data volume — your SQLite database and all workflow JSON lives here

Why restart: always Matters

In automation, uptime is revenue. Consider this scenario: your Contabo VPS in Germany receives a scheduled maintenance reboot at 3 AM. Without restart: always, your n8n instance stays down until you SSH in and start it manually — potentially hours later. With restart: always, Docker brings it back up within 30 seconds automatically. For Pakistani clients relying on order processing workflows, those hours of downtime are PKR directly lost.

VPS Selection Guide for Pakistan (2026 Rates)

ProviderPlanRAMPrice/monthBest For
Contabo VPS SStandard8GB$7.501-3 clients, learning
Contabo VPS MStandard16GB$13.504-10 clients, production
DigitalOceanBasic Droplet2GB$12Lowest latency to Southeast Asia
Hostinger KVM 2VPS4GB$8.99Budget option, decent uptime
Vultr High Performance1 vCPU2GB$12Fast NVMe, Singapore node available

Payment tip for Pakistani developers: Most of these accept PayPal, Wise, or Payoneer. If your local bank card gets rejected (common with Meezan, UBL, Habib cards on foreign sites), use Payoneer or create a Wise virtual card — standard practice in the Karachi freelance community.

Recommended for beginners: Contabo VPS S ($7.50/month). Its Germany location provides good latency to both European and Pakistani traffic. 8GB RAM easily handles 10+ concurrent n8n workflow executions.

Practice Lab

Practice Lab: Your First Container

Exercise 1: Install and Verify Docker

bash
# Install Docker Desktop (Windows/Mac) or Docker Engine (Linux)
# After installation, verify:
docker --version
# Expected: Docker version 27.x.x or higher

docker run hello-world
# Expected: "Hello from Docker!" message

Exercise 2: Run Your First Web Container

bash
docker run -d -p 8080:80 nginx

Visit http://localhost:8080 — you are now hosting an Nginx web server in an isolated container. Kill it:

bash
docker stop $(docker ps -q --filter ancestor=nginx)

Exercise 3: Launch n8n with docker-compose

bash
# Create your n8n directory
mkdir -p /opt/n8n && cd /opt/n8n

# Create the docker-compose.yml (paste the content from this lesson)

# Start n8n in the background
docker-compose up -d

# Verify it's running
docker-compose ps
# Expected: n8n   Up   0.0.0.0:5678->5678/tcp

# View live logs
docker-compose logs -f n8n

Visit http://localhost:5678 (or your VPS IP:5678). Create your owner account. Your automation factory is live.

Pakistan Case Study: Sana's First VPS

Character: Sana Mirza, 24, graphic designer from Lahore who transitioned into automation freelancing after learning n8n.

The situation: Sana built her first client workflow (a lead-gen pipeline for a Lahore real estate agency) on her local laptop. The client asked: "Can this run 24/7?" Sana had never set up a server before.

What she did:

  1. Signed up for Contabo VPS S at $7.50/month (paid via Payoneer)
  2. SSH'd into her Ubuntu 22.04 server for the first time
  3. Installed Docker with one command: curl -fsSL https://get.docker.com | sh
  4. Copied her docker-compose.yml and ran docker-compose up -d
  5. n8n was live at her server IP within 8 minutes

The result: Her client's workflow now runs 24/7 in Germany while Sana sleeps in Lahore. She charges PKR 8,000/month maintenance retainer. The VPS costs PKR 2,100/month. Net profit: PKR 5,900/month passive income — from a workflow she built once.

Sana's reflection: "Pehle Docker scary lagta tha. Phir ek baar kar lia toh realize kiya — yeh toh sirf ek command hai. Docker sikhna meri best investment thi."

Key Takeaways

  • Docker images are read-only blueprints; containers are running instances — understand this distinction before everything else
  • Named volumes (n8n_data) are mandatory in production — without them, a container restart wipes all your workflows
  • restart: always ensures n8n survives VPS reboots and crashes without manual intervention
  • Set GENERIC_TIMEZONE=Asia/Karachi so all schedule triggers fire in PKT, not UTC — a 5-hour miscalculation breaks every timed automation
  • Contabo VPS S ($7.50/month) is the community standard for n8n in Pakistan — 8GB RAM handles 10+ concurrent workflows easily
  • Pakistani developers can pay for VPS via Payoneer or Wise when bank cards are rejected on foreign hosting sites
  • docker-compose up -d is the single command that launches your entire automation factory — master it early
  • The difference between a self-hosted n8n and a cloud SaaS is the difference between owning your tools and renting them

Recommended Videos and Resources

Mini-Challenge

Get Docker running today: Install Docker Desktop, run docker run hello-world, then pull the n8n image with docker pull n8nio/n8n. Launch it with docker-compose using the YAML from this lesson. Access the UI at localhost:5678 and create your owner account. Time yourself — can you go from zero to a running n8n instance in under 15 minutes?

Lesson Summary

Includes hands-on practice lab6 runnable code examples5-question knowledge check below

Quiz: Docker Setup & Deployment

5 questions to test your understanding. Score 60% or higher to pass.