Skip to content

About

Declarative Headscale + Headplane Docker Compose stack with an optional Caddy reverse proxy for automated HTTPS.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

Headscale Stack

Declarative Headscale + Headplane Docker Compose stack with an optional Caddy reverse proxy for automated HTTPS.

Docker Compose Headscale Headplane Caddy License


Features • Architecture • Quick Start • Connecting Devices • Reverse Proxy

Headplane Web UI Dashboard

Features

  • 100% Declarative & GitOps-Ready: No host-level bash scripts or runtime string manipulations. The entire stack is configured via a single .env file.
  • Modern Web Interface: Bundled with Headplane for visual node management, ACL configuration, and read-only MagicDNS & config view.
  • Zero-Config Automatic TLS: Built-in Caddy reverse proxy provisions and renews Let's Encrypt / ZeroSSL certificates automatically.
  • Flexible Ingress: Run the built-in Caddy proxy (COMPOSE_PROFILES=caddy) or seamlessly connect your existing Nginx Proxy Manager, Traefik, or host Nginx.
  • Container Health & Order: Headscale health checks (headscale health) ensure Headplane boots strictly when the control plane and database are ready.
  • Safe Data Persistence: SQLite databases, TLS certificates, and session data live in isolated Docker Named Volumes—no host permission issues (UID/GID).
  • Tailscale Compliant: Implements real client IP forwarding (True-Client-IP), WebSocket streaming, and mobile captive portal detection (/generate_204).

Architecture

                     ┌────────────────────────────────────────┐
                     │          Internet / Clients            │
                     └───────────────────┬────────────────────┘
                                         │
            443/tcp+udp (HTTPS/H3)       │        3478/udp (STUN)
             ┌───────────────────────────┴───────────────────────────────┐
             ▼                                                           │
   ┌───────────────────┐                                                 │
   │   Reverse Proxy   │                                                 │
   │   (Caddy / NPM)   │                                                 │
   └─────────┬─────────┘                                                 │
             │                                                           │
   ┌─────────┴──────────────────────┐                                    │
   │ /admin*                        │ /*                                 │
   ▼                                ▼                                    ▼
┌──────────────────────┐   ┌────────────────────────────────────────────────┐
│      Headplane       │──>│                   Headscale                    │
│       (Web UI)       │   │          (Control Plane / Hub / STUN)          │
└──────────┬───────────┘   └───────────────────────┬────────────────────────┘
           ▼                                       ▼
    [headplane-data]                        [headscale-data]

Quick Start

1. Clone the repository

git clone https://github.com/mikhailde/headscale-stack.git
cd headscale-stack

2. Prepare environment variables

cp .env.example .env

Edit .env and configure your settings:

DOMAIN=headscale.example.com
COMPOSE_PROFILES=caddy

Generate a secure 32-character hex secret for Headplane:

openssl rand -hex 16

(Copy the output into HEADPLANE_COOKIE_SECRET)

Generate a bcrypt password hash for Caddy Basic Auth:

docker run -it --rm caddy:alpine caddy hash-password

(Paste into ADMIN_PASSWORD_HASH in .env. Note: escape each $ as $$, e.g. $$2a$$14$$...)

Warning

Cloudflare DNS Users: If using Cloudflare DNS, set your DNS record proxy status to DNS-only (grey cloud). Cloudflare Proxy blocks Tailscale WebSocket POST requests required by the protocol.

3. Launch the stack

docker compose up -d

4. Create an API Key & Access the Dashboard

Generate a Headscale API key for Headplane:

docker compose exec headscale headscale apikeys create --expiration 90d

Open https://<your-domain>/admin/ in your browser:

  1. Log in via HTTP Basic Auth using your configured ADMIN_USER and password.
  2. Enter the generated API key into Headplane to access the dashboard.
  3. Create your first user under Users (e.g. default). Clients require an existing user to register.
  4. Configure and save an access policy under Access Control (ACL) before connecting devices. By default, policy.mode: database permits all-to-all traffic until a policy is saved.

Connecting Devices

Linux / macOS / Windows

Run the following command in your terminal:

tailscale up --login-server https://<your-domain>

To authenticate automatically with a pre-auth key:

tailscale up --login-server https://<your-domain> --auth-key <preauth-key>

iOS / Android

  1. Open the official Tailscale app.
  2. Open the menu → Settings → Accounts → Use custom control server.
  3. Enter your server URL: https://<your-domain>.
Client Deployment Recipes (Exit Nodes & Server Nodes)

1. Tailscale Exit Node (Full VPN Gateway 0.0.0.0/0)

Use this recipe to turn a remote VPS into an exit node for browsing the internet:

services:
  tailscale-exit:
    image: tailscale/tailscale:stable
    container_name: tailscale-exit
    restart: unless-stopped
    devices:
      - /dev/net/tun:/dev/net/tun
    cap_add:
      - NET_ADMIN
      - NET_RAW
    sysctls:
      - net.ipv4.ip_forward=1
      - net.ipv6.conf.all.forwarding=1
    ports:
      - "41641:41641/udp"
    volumes:
      - tailscale-state:/var/lib/tailscale
    environment:
      - TS_AUTHKEY=replace_with_preauth_key
      - TS_AUTH_ONCE=true
      - TS_HOSTNAME=exit-node
      - TS_USERSPACE=false
      - TS_STATE_DIR=/var/lib/tailscale
      - TS_NO_LOGS_NO_SUPPORT=true
      - TS_EXTRA_ARGS=--login-server=https://<your-domain> --advertise-exit-node
volumes:
  tailscale-state:

Linux performance tip (enables UDP GRO forwarding on the host to prevent exit-node speed throttling):

NETDEV=$(ip -o route get 8.8.8.8 | cut -f 5 -d " ")
sudo ethtool -K "$NETDEV" rx-udp-gro-forwarding on rx-gro-list off

2. Tailscale Server Node (Host Network Endpoint)

Use this recipe to join any Docker host directly into your private tailnet:

services:
  tailscale-server:
    image: tailscale/tailscale:stable
    container_name: tailscale-server
    restart: unless-stopped
    network_mode: host
    devices:
      - /dev/net/tun:/dev/net/tun
    cap_add:
      - NET_ADMIN
      - NET_RAW
    volumes:
      - tailscale-state:/var/lib/tailscale
    environment:
      - TS_AUTHKEY=replace_with_preauth_key
      - TS_AUTH_ONCE=true
      - TS_HOSTNAME=server-main
      - TS_USERSPACE=false
      - TS_STATE_DIR=/var/lib/tailscale
      - TS_NO_LOGS_NO_SUPPORT=true
      - TS_EXTRA_ARGS=--login-server=https://<your-domain>
volumes:
  tailscale-state:

Note: With TS_AUTH_ONCE=true, changes to TS_EXTRA_ARGS are ignored on container restarts after initial registration.

Reverse Proxy Options

Option A: Built-in Caddy (Default)

Keep COMPOSE_PROFILES=caddy in .env. Caddy manages Let's Encrypt certificates, HTTP-to-HTTPS redirects, /generate_204 responses, and basic auth out of the box.

Option B: External Reverse Proxy (Nginx Proxy Manager, Traefik, Host Nginx)

  1. In .env, set COMPOSE_PROFILES= (empty string).
  2. Connect your external proxy container to the stack network:
    docker network connect headscale-net <your-proxy-container-name>
  3. Configure your proxy rules:
Route Target Protocol Notes
https://<domain>/admin* http://headplane:3000 HTTP Enable WebSockets
https://<domain>/* http://headscale:8080 HTTP Enable WebSockets, pass True-Client-IP

Notes:

  • Nginx Proxy Manager (NPM): Enable Websockets Support and leave Cache Assets and Block Common Exploits disabled.
  • Host-installed proxy: Skip step 2, bind 127.0.0.1:8080:8080 (headscale) and 127.0.0.1:3000:3000 (headplane) via compose.override.yaml, and target 127.0.0.1 instead of container names.
View Nginx configuration snippet
# Pass real client IP and WebSocket upgrades
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header True-Client-IP $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering off;
proxy_read_timeout 1d;
proxy_send_timeout 1d;

# Captive portal detection (place in port 80 server block before HTTPS redirect)
location = /generate_204 {
    return 204;
}

# Headplane Web UI
location = /admin {
    return 301 /admin/;
}
location ^~ /admin/ {
    proxy_pass http://headplane:3000;
}

# Headscale VPN Control Plane
location / {
    proxy_pass http://headscale:8080;
}

DERP (Relay) Configuration

Headscale allows relaying traffic when direct WireGuard P2P connections cannot be established through strict NATs:

  • Embedded DERP Server: Controlled via DERP_SERVER_ENABLED=true/false in .env. When enabled, Headscale serves STUN on port 3478/udp. If you also use an Exit Node, set derp.server.ipv4 / derp.server.ipv6 in config/headscale.yaml to avoid a DNS deadlock on reconnect.
  • Global Tailscale DERP Map: Configured in config/headscale.yaml under derp.urls.
    • To run in 100% private / air-gapped mode without sending relay traffic to Tailscale Inc servers, set:
      derp:
        urls: []

Security & Maintenance

Read-Only Configuration

config/headscale.yaml and config/headplane.yaml are mounted read-only (:ro). This adheres to GitOps principles: configuration changes are version-controlled in Git rather than modified ad-hoc from a web browser.

Single Sign-On (OIDC/SSO)

If you configure Headplane with an OIDC provider (Google, GitHub, Keycloak), make sure to remove or bypass Caddy Basic Auth. Basic Auth will intercept /admin/oidc/callback and cause OAuth authentication flow failures.

Upgrading

Always back up your data before upgrading. Headscale only supports one minor version upgrade at a time (e.g. 0.28 → 0.29); avoid skipping minor releases when pulling new images on an existing database.

Backup & Restoration

All persistent VPN state (SQLite database and encryption keys) is stored in /var/lib/headscale. (If you enable OIDC in Headplane, also back up /var/lib/headplane.)

Backup

Briefly stop the service to flush the SQLite WAL journal into the main database, then copy the data directory directly from the container:

docker compose stop headscale
docker compose cp headscale:/var/lib/headscale ./headscale_backup_$(date +%F_%H-%M)
docker compose start headscale

Restore

To restore from backup directory (restore into a clean volume, or remove any stale db.sqlite-wal / db.sqlite-shm files first):

docker compose stop headscale
docker compose cp ./headscale_backup_<timestamp>/. headscale:/var/lib/headscale/
docker compose start headscale

Show Your Support

If this stack saved you time, please consider giving it a ⭐ Star — it helps more people discover the project!

References & Acknowledgements

Note: The Docker Compose stack and configurations are entirely hand-crafted. An LLM was used solely to format and proofread this documentation.

License

This project is licensed under the MIT License.

About

Declarative Headscale + Headplane Docker Compose stack with an optional Caddy reverse proxy for automated HTTPS.

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Contributors