Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
2a9c719baThis file is pinned to the selected release. Download the original to open it in your design tool.
# HiveHub — Docker Installation Guide
This guide covers deploying the HiveHub backend on any Linux system with Docker and Docker Compose. The process is essentially the same as the [TrueNAS installation](truenas-install.md), but without the TrueNAS-specific UI steps.
---
## Prerequisites
- A Linux server or VPS (Ubuntu 22.04+ / Debian 12+ recommended)
- [Docker Engine](https://docs.docker.com/engine/install/) ≥ 24
- [Docker Compose](https://docs.docker.com/compose/install/) ≥ 2 (usually bundled as `docker compose`)
- A directory for persistent database storage (e.g. `/opt/hivescale/db`)
- If you use [hive audio](audio-recording.md), a directory for recordings (e.g. `/opt/hivescale/recordings`) — roughly 1.9 MB per recorded minute, kept until you delete it
---
## Step 1 — Create storage directories
```bash
sudo mkdir -p /opt/hivescale/db
sudo mkdir -p /opt/hivescale/firmware # optional: for OTA binary hosting
sudo mkdir -p /opt/hivescale/recordings # optional: for hive audio recordings
```
---
## Step 2 — Create the environment file
```bash
cd /opt/hivescale
cp /path/to/repo/docker/.env.example .env
nano .env
```
Fill in all values:
```env
# Master/admin key (X-API-Key header) for server-to-server tooling:
# firmware-release registration, command queueing, latest-measurements, time.
# Devices no longer need this — each device registers its own per-device key.
API_KEY=change-this-to-a-long-random-string
# Service key used by HivePal (X-HivePal-Service-Key header)
HIVEPAL_SERVICE_API_KEY=change-this-to-another-long-random-string
# Shared secret (HS256) used to verify HivePal's per-user Bearer tokens.
# Must match HivePal's JWT_SECRET. Required for HivePal app endpoints.
HIVEPAL_JWT_SECRET=change-this-to-match-hivepal-jwt-secret
# PostgreSQL password — must match POSTGRES_PASSWORD below
POSTGRES_PASSWORD=change-this-database-password
# Public base URL used to build OTA firmware download links
# Use HTTPS in production (behind your reverse proxy / Cloudflare)
PUBLIC_BASE_URL=https://your-domain.example
# Timezone for logs and timestamps
TZ=Europe/Berlin
# ── Abuse / DoS protection (optional — sensible defaults shown) ──
# Per-client-IP request rate limit; set RATE_LIMIT_ENABLED=false to disable
RATE_LIMIT_ENABLED=true
RATE_LIMIT_DEFAULT=120/minute
# Max JSON request body (bytes); 262144 = 256 KiB
MAX_BODY_BYTES=262144
# Max uploaded firmware binary (bytes); 16777216 = 16 MiB
MAX_FIRMWARE_BYTES=16777216
# Trust CF-Connecting-IP / X-Forwarded-For — set true ONLY behind a reverse
# proxy that overwrites them (see "Exposing to the internet" below)
TRUST_PROXY_HEADERS=false
```
> Generate strong random keys with:
> ```bash
> openssl rand -hex 32
> ```
---
## Step 3 — Create the Docker Compose file
Copy the compose file from the repo or create `/opt/hivescale/docker-compose.yml`:
```yaml
services:
hivescale-api:
image: ghcr.io/macnite/hivehub:latest
depends_on:
hivescale-db:
condition: service_healthy
environment:
API_KEY: ${API_KEY}
HIVEPAL_SERVICE_API_KEY: ${HIVEPAL_SERVICE_API_KEY}
HIVEPAL_JWT_SECRET: ${HIVEPAL_JWT_SECRET}
DATABASE_URL: postgresql://hivescale:${POSTGRES_PASSWORD}@hivescale-db:5432/hivescale
PUBLIC_BASE_URL: ${PUBLIC_BASE_URL}
FIRMWARE_DIR: /app/firmware
RECORDINGS_DIR: /app/recordings
TZ: ${TZ}
# Abuse/DoS protection (optional — defaults applied if omitted)
RATE_LIMIT_ENABLED: ${RATE_LIMIT_ENABLED:-true}
RATE_LIMIT_DEFAULT: ${RATE_LIMIT_DEFAULT:-120/minute}
MAX_BODY_BYTES: ${MAX_BODY_BYTES:-262144}
MAX_FIRMWARE_BYTES: ${MAX_FIRMWARE_BYTES:-16777216}
# Off by default — set true ONLY behind a reverse proxy that overwrites
# these headers (see "Exposing to the internet" below).
TRUST_PROXY_HEADERS: ${TRUST_PROXY_HEADERS:-false}
ports:
# Loopback-only by default: reach the API through a reverse proxy that
# terminates TLS. Change to "31115:8000" to expose it directly on the LAN
# (not recommended — set TRUST_PROXY_HEADERS=false if you do).
- "127.0.0.1:31115:8000"
volumes:
- /opt/hivescale/firmware:/app/firmware
# Hive audio. Recordings are files on disk, and the database rows that
# describe them survive a container recreation — so without this mount a
# redeploy leaves a list of sessions whose audio is gone. There is no
# retention sweep either: budget ~1.9 MB per minute of recording.
- /opt/hivescale/recordings:/app/recordings
restart: unless-stopped
hivescale-db:
image: postgres:16-alpine
environment:
POSTGRES_DB: hivescale
POSTGRES_USER: hivescale
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
TZ: ${TZ}
healthcheck:
test:
- CMD-SHELL
- pg_isready -U hivescale -d hivescale
interval: 30s
timeout: 5s
retries: 5
restart: unless-stopped
volumes:
- /opt/hivescale/db:/var/lib/postgresql/data
```
---
## Step 4 — Start the stack
```bash
cd /opt/hivescale
docker compose up -d
```
Check that both containers are running:
```bash
docker compose ps
```
View live logs:
```bash
docker compose logs -f
```
---
## Step 5 — Verify
```bash
curl http://localhost:31115/health
```
Expected:
```json
{ "status": "ok" }
```
The port is bound to loopback by default, so `localhost` works from the server
itself but `http://your-server-ip:31115` will **not** be reachable from another
machine until you put a reverse proxy in front of it (see
[Exposing to the internet](#exposing-to-the-internet-optional)). Interactive API
docs are then served at `https://your-domain.example/docs` through the proxy (or
`http://localhost:31115/docs` on the server itself).
---
## Step 6 — Configure the firmware
Edit `firmware/include/secrets.h`:
```cpp
#define API_BASE_URL "https://your-domain.example" # HTTPS — the firmware verifies the TLS cert
#define API_KEY "a-unique-per-device-key" # generate with: openssl rand -hex 32
```
Give each device a unique `API_KEY`; the backend registers it against the
device's `device_id` on first contact. Re-flash the device, or update the key
via the provisioning portal if it is already deployed.
> **HTTPS is required by default.** The firmware verifies the backend's TLS certificate
> against the Let's Encrypt root CA bundled in `firmware/include/ca_cert.h`, so
> `API_BASE_URL` must point at an HTTPS endpoint with a valid certificate — see
> [Exposing to the internet](#exposing-to-the-internet-optional). For a different
> CA, replace the certificate in `ca_cert.h`.
> **Trusted-LAN HTTP opt-in:** If TLS is unavailable on an isolated trusted LAN,
> set `#define ALLOW_INSECURE_HTTP 1` in `secrets.h` and use an `http://`
> `API_BASE_URL` such as `http://192.168.1.10:31115`. HTTPS remains available
> and certificate-verified with this setting enabled. HTTP exposes device
> credentials and OTA traffic; do not enable it on an untrusted network.
---
## OTA firmware updates
Place compiled firmware binaries in `/opt/hivescale/firmware/`, then register them:
```bash
curl -X POST http://localhost:31115/api/v1/firmware/releases \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"version": "0.5.0", "filename": "hivescale-0.5.0.bin", "active": true, "target": "hivescale"}'
```
`target` defaults to `hivescale`; `hiveinside` and `beecounter` publish images
the HiveHub relays over BLE GATT to a paired HiveInside sensor or HiveTraffic
counter respectively. App clients (HivePal) can also upload
the binary directly via `POST /api/v1/app/devices/{id}/firmware` instead of
copying it into `FIRMWARE_DIR` first — see [api.md](api.md).
The device checks for updates every 6 hours and on every measurement cycle.
---
## Updating the application
```bash
cd /opt/hivescale
docker compose pull
docker compose up -d
```
The database volume is preserved — no data is lost on updates.
---
## Backups
**Logical dump (recommended):**
```bash
docker compose exec hivescale-db pg_dump -U hivescale hivescale > hivescale-backup-$(date +%Y%m%d).sql
```
**Restore from dump:**
```bash
docker compose exec -T hivescale-db psql -U hivescale hivescale < hivescale-backup-20260501.sql
```
---
## Exposing to the internet (optional)
The compose file binds the API to **loopback (`127.0.0.1:31115`) by default**, so
it is not directly reachable from the network until you deliberately put
something in front of it. The firmware also verifies the backend's TLS
certificate, so devices must reach the API over **HTTPS with a valid
certificate**. The recommended setup:
- **Reverse proxy with HTTPS (recommended)** — run Nginx or Caddy on the same host and forward to `127.0.0.1:31115`; obtain a TLS certificate via Let's Encrypt. Caddy does this automatically with a single config line. The bundled `firmware/include/ca_cert.h` already trusts the Let's Encrypt root, so no firmware change is needed.
- **Tailscale / WireGuard** — put both the server and the ESP32 (via a companion device or router) on a private VPN. You still need a valid TLS certificate the device will trust on the address it connects to.
- **Port forwarding** — if you must expose the port directly, change the mapping from `127.0.0.1:31115:8000` back to `31115:8000` and forward it on your router, but still terminate TLS in front of it (e.g. via a proxy) so the device can verify the certificate; the firmware rejects a plain-HTTP endpoint or an untrusted certificate. Leave `TRUST_PROXY_HEADERS=false` in this case.
> **Enable proxy-header trust behind the proxy.** Per-client rate limiting keys
> off the client IP. With the API behind a reverse proxy, set
> `TRUST_PROXY_HEADERS=true` so it reads the real client IP from
> `CF-Connecting-IP` / `X-Forwarded-For` instead of lumping everyone under the
> proxy's address. This is **off by default** because trusting those headers is
> only safe when a proxy overwrites them — if the API is reachable directly, a
> client can spoof them to dodge the limiter. Make sure the proxy actually sets
> the forwarded-IP header and does not pass a client-supplied one through.
> **Rate limiting & request size:** the API enforces a per-client-IP rate limit
> and a request-body cap out of the box (see the environment variables above).
> For defence in depth, also enable limits at the proxy — e.g. Nginx `limit_req`
> + `client_max_body_size 256k`, Caddy `rate_limit`, or a Cloudflare WAF
> rate-limiting rule on `/api/v1/*`. Set Cloudflare SSL/TLS mode to
> **Full (strict)**.
---
## Troubleshooting
**Containers exit on startup:**
```bash
docker compose logs hivescale-api
docker compose logs hivescale-db
```
Common causes: wrong `DATABASE_URL` password, missing environment variable, or port `31115` already in use.
**Port already in use:**
```bash
sudo ss -tlnp | grep 31115
```
Change the host-side port mapping in the compose file (e.g. `"127.0.0.1:31116:8000"`) if something else is using `31115`.
**Measurements not stored:**
Use the test commands in [test-commands.md](test-commands.md) to verify the API key and payload format.
**OTA firmware URL does not resolve:**
Ensure `PUBLIC_BASE_URL` is set to an address the ESP32 can reach — not `localhost`.