Skip to content

docker-compose.yml

Download docker-compose.yml as it is.

deploy/compose/docker-compose.yml
# Hour Advantage as a cluster: HAProxy in front of N gateway instances sharing
# one Postgres and one Valkey (the cache and the messages between the instances). See the Docker Compose guide in the Hour Advantage docs.
#
# Exposed ports are deploy config, listeners are runtime config (kept in
# Postgres): this file lists the exact device ports, and a listener on a new
# port needs that port added here and in haproxy.cfg too
# (`hourctl export lb compose` / `... haproxy` print both).
#
# (load the Hour Advantage image from your distribution first, see .env.example)
# cp .env.example .env # then fill in the passwords, the login and the key
# docker compose up -d --wait
# docker compose run --rm packages # push the image's package bundles
# docker compose up -d --scale gateway=5
#
# Needs Docker Compose 2.24 or newer (secrets from the environment, !reset
# in docker-compose.external-db.yml) and Docker Engine 25 or newer (the
# health checks' start_interval).
#
# HAProxy finds the gateway instances through Docker's DNS (the service name
# "gateway" resolves to every running replica), so scaling needs no change
# to its configuration. To move the gateways to a new image without turning
# clocks away, use rolling-update.ps1 (or .sh), not `up -d`, which recreates
# every replica at once.
#
# A replica that is killed (`docker kill`, an out-of-memory kill) is not
# started again by `restart: unless-stopped`, which restarts a container
# only when its own process fails; Docker leaves one that was killed from
# outside as stopped. To bring the stack back to the replicas it asks for:
# docker compose up -d --no-recreate --wait
name: houradvantage
x-labels: &labels
# Keep reverse proxies and auto-updaters that watch the Docker socket on
# this machine away from these containers.
traefik.enable: "false"
com.centurylinklabs.watchtower.enable: "false"
# The Hour Advantage containers: no capabilities, no privilege gain. (Not
# read_only: compose copies the secrets below into the container, which a
# read-only root refuses. Kubernetes runs the same image read-only.)
x-hardening: &hardening
cap_drop: [ALL]
security_opt: ["no-new-privileges:true"]
# Docker keeps what a container prints in a file per container; without a
# limit it grows until the disk is full.
x-logging: &logging
driver: json-file
options:
max-size: ${LOG_MAX_SIZE:-20m}
max-file: "${LOG_MAX_FILES:-5}"
services:
postgres:
# Pinned by digest (the tag says which version it is); a newer patch is
# taken by changing both.
image: postgres:17-alpine@sha256:eee6c6e1b0894fa4fe0075eb054af9fd1cf39a35321c2d4110626cfe506411fb
restart: unless-stopped
environment:
POSTGRES_DB: houradvantage
POSTGRES_USER: houradvantage
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets: [postgres_password]
# Room for every gateway's pool: replicas x (pool size + 2), the pool
# being 4 per CPU, between 8 and 32, plus a few for administration. Raise it with the replicas.
# The keepalives every connection of the gateways must carry over TCP (they
# refuse to start without them): the server finds a connection whose host
# has gone within half a minute and lets its locks go, so that what waits
# on them goes on. Nothing ends a wait by the time it has taken (no
# statement_timeout, lock_timeout or idle_in_transaction_session_timeout:
# the gateways refuse a database that has one). Here they are the
# server's defaults; on a database you run yourself they belong to the
# role (Operations, Database role settings), which is what a pooler such
# as PgBouncer in transaction mode needs.
command:
- postgres
- -c
- max_connections=${POSTGRES_MAX_CONNECTIONS:-200}
- -c
- tcp_keepalives_idle=10
- -c
- tcp_keepalives_interval=5
- -c
- tcp_keepalives_count=3
- -c
- tcp_user_timeout=30s
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U houradvantage -d houradvantage"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
# Not published: only the gateways need it. To reach it from the host,
# uncomment (loopback only):
# ports:
# - "127.0.0.1:25432:5432"
networks: [houradvantage]
logging: *logging
labels: *labels
# What the instances share besides the database: their cache of what they keep of the devices, and the messages by which they
# tell each other of a change. Nothing in it is kept: everything in it is rebuilt from Postgres, so it runs with no
# persistence (a restart is a cold cache and a resync, not a loss). An instance with none refuses to start beside another, and
# one with a Valkey it cannot reach reads from Postgres until it can. One server here; for a Valkey that survives the loss
# of a host, point HOURADVANTAGE_CACHE_URL at a Sentinel (redis-sentinel://) or a managed Valkey and drop this service.
# Not published: only the gateways need it.
valkey:
# Pinned by digest (the tag says which version it is).
image: valkey/valkey:8.1-alpine@sha256:2602ace2be6569bc2bbb06f873776516ab9089821525be34f4305c67eb9db02a
restart: unless-stopped
# As the image's own user: started as root the image's entrypoint drops to it with setuid, which no capability left refuses.
user: "999:1000"
command: ["valkey-server", "--save", "", "--appendonly", "no"]
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
<<: *hardening
networks: [houradvantage]
logging: *logging
labels: *labels
gateway:
# The Hour Advantage image from the distribution (HOURADVANTAGE_IMAGE in .env).
image: ${HOURADVANTAGE_IMAGE:-houradvantage:0.1.0}
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
# The image listens for the admin API on 0.0.0.0:9090 (reachable on the
# compose network by HAProxy's checks, Prometheus and setup; never
# published on the host). The gateway logs JSON lines to stdout (it is
# not a terminal) and drains for 6 s on SIGTERM, longer than HAProxy
# takes to stop sending it work.
#
# The secrets come from .env (or your environment) as files in
# /run/secrets, read through the *_FILE variables, so they are not in
# the containers' environment (`docker inspect`). Compose refuses to
# start the gateways while one is not set.
environment:
HOURADVANTAGE_DATABASE_URL_FILE: /run/secrets/database_url
# The Valkey the instances share (see the valkey service), and the install's id in it (the first part of every key). With
# it any number of instances share the database; without it an instance is the only one.
HOURADVANTAGE_CACHE_URL: ${HOURADVANTAGE_CACHE_URL:-redis://valkey:6379}
HOURADVANTAGE_CACHE_INSTALL: ${HOURADVANTAGE_CACHE_INSTALL:-default}
# Required: the admin API listens beyond loopback, and the gateway
# refuses to start without a login.
HOURADVANTAGE_ADMIN_USERNAME: ${HOURADVANTAGE_ADMIN_USERNAME:?set HOURADVANTAGE_ADMIN_USERNAME in .env}
HOURADVANTAGE_ADMIN_PASSWORD_HASH_FILE: /run/secrets/admin_password_hash
# Seals the secrets kept in the database; the same for every instance
# (see .env.example). The instances also prove themselves to each
# other with a token derived from it when one relays a live view
# (`hourctl wire`, `hourctl stream`). They reach each other's admin API (9090)
# on this network at the address each finds for itself
# (HOURADVANTAGE_ADVERTISE_ADDR overrides it).
HOURADVANTAGE_SECRET_KEY_FILE: /run/secrets/secret_key
# Only while changing the key (.env.example): a secret like the key,
# empty otherwise.
HOURADVANTAGE_SECRET_KEYS_OLD_FILE: /run/secrets/secret_keys_old
# Time and Attendance beside Connect on every instance: on or off
# (.env.example).
HOURADVANTAGE_ATTEND: ${HOURADVANTAGE_ATTEND:-off}
secrets: [database_url, admin_password_hash, secret_key, secret_keys_old]
volumes:
# The cluster's listeners and endpoints, for `hourctl apply`:
# docker compose exec -e HOURADVANTAGE_ADMIN_PASSWORD gateway hourctl apply /etc/houradvantage/setup.toml
# (hourctl logs in as HOURADVANTAGE_ADMIN_USERNAME; the password is given per
# command.) It also sets trusted_proxies to this network, so the
# gateways believe X-Forwarded-For from HAProxy and see each clock's
# own address.
- ./houradvantage-setup.toml:/etc/houradvantage/setup.toml:ro
# No ports: devices reach the instances only through HAProxy, over the
# compose network, where every port of a container is reachable. The
# device ports are listed on the haproxy service below.
deploy:
replicas: ${GATEWAY_REPLICAS:-3}
# Each instance uses the processors it is given: worker threads, Lua
# concurrency and the database pool all follow this limit.
resources:
limits:
cpus: "${GATEWAY_CPUS:-2}"
memory: ${GATEWAY_MEMORY:-1g}
reservations:
memory: ${GATEWAY_MEMORY_RESERVED:-256m}
# Healthy = takes work: GET /api/ready on the admin port, the same check
# HAProxy makes (instead of the image's /livez), so `up --wait` returns
# once every instance would be put in rotation.
healthcheck:
test: ["CMD", "hourd", "healthcheck", "--ready"]
interval: 5s
timeout: 3s
retries: 3
start_period: 60s
start_interval: 1s
# The gateway stops in about 20 s at the most (drain 6 s, listeners 5 s,
# the attendance workers ending a batch up to 15 s alongside, the admin
# API 5 s); twice that is the room.
stop_grace_period: 40s
<<: *hardening
networks: [houradvantage]
logging: *logging
labels: *labels
# One-shot, run by hand: `docker compose run --rm packages`. Pushes and
# activates the package bundles the image carries
# (/usr/share/houradvantage/packages), then exits. Packages are kept in
# Postgres, so this is done once for every instance, and again to install
# the bundles of a new image after a rolling update. Pushing is
# idempotent (the same bundle is the same revision, and activating the
# active revision changes nothing). It logs in with
# HOURADVANTAGE_ADMIN_USERNAME and HOURADVANTAGE_ADMIN_PASSWORD (the password
# itself, from .env or your environment). In the `setup` profile, so `up`
# leaves it alone and `up --wait` waits for the long-running services
# only.
packages:
image: ${HOURADVANTAGE_IMAGE:-houradvantage:0.1.0}
profiles: [setup]
restart: "no"
depends_on:
gateway:
condition: service_healthy
# A shell, for the glob.
entrypoint: ["sh", "-c"]
command: ["exec hourctl pkg push --activate /usr/share/houradvantage/packages/*.tar.gz"]
environment:
HOURADVANTAGE_ADMIN_URL: http://gateway:9090
# The admin API is reached over the compose network, which is private,
# so hourctl may send the login over plain http://.
HOURADVANTAGE_INSECURE: "true"
HOURADVANTAGE_ADMIN_USERNAME: ${HOURADVANTAGE_ADMIN_USERNAME:?set HOURADVANTAGE_ADMIN_USERNAME in .env}
HOURADVANTAGE_ADMIN_PASSWORD_FILE: /run/secrets/admin_password
secrets: [admin_password]
<<: *hardening
networks: [houradvantage]
logging: *logging
labels: *labels
haproxy:
image: haproxy:3.2-alpine@sha256:dfba86f031bd4c9039f10f8cc5de75991d1b6766d5460f0335b980408503baad
restart: unless-stopped
depends_on:
gateway:
condition: service_started
volumes:
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
ports:
# Device ports, one line each, matching a frontend in haproxy.cfg:
# 8080 is the HTTP listener of houradvantage-setup.toml, published on the
# host as 28080 (change the left side to taste, e.g. "8080:8080").
# No MQTT port: MQTT listeners connect out to a broker, which is not
# part of this stack.
- "28080:8080"
# HAProxy stats page and CSV, loopback only.
- "127.0.0.1:${HAPROXY_STATS_PORT:-28404}:8404"
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/healthz"]
interval: 10s
timeout: 3s
retries: 3
networks: [houradvantage]
logging: *logging
labels: *labels
networks:
houradvantage:
ipam:
config:
# Fixed so the gateways can be told to trust X-Forwarded-For from
# this network only (trusted_proxies in houradvantage-setup.toml).
- subnet: ${COMPOSE_SUBNET:-172.30.28.0/24}
volumes:
pgdata:
# Each from the variable of that name in .env or your environment, handed
# to the containers that use it as a file in /run/secrets.
secrets:
postgres_password:
environment: POSTGRES_PASSWORD
database_url:
environment: HOURADVANTAGE_DATABASE_URL
admin_password_hash:
environment: HOURADVANTAGE_ADMIN_PASSWORD_HASH
secret_key:
environment: HOURADVANTAGE_SECRET_KEY
secret_keys_old:
environment: HOURADVANTAGE_SECRET_KEYS_OLD
admin_password:
environment: HOURADVANTAGE_ADMIN_PASSWORD