Repository navigation
How it works
Two things the picture does not show:
-
docker bouncerdrives each proxy throughdocker exec: it rewrites the proxy's cluster list (which replicas Envoy may send traffic to) and reads Envoy's health and connection state. Nothing listens on the host for it. - While an
uporundoruns, the project's lock container<project>-bouncer-lockexists. It is created but never started; its unique name is the mutex.
There is no state store. Every run derives what it needs from three sources: the compose file, container labels, and Envoy's live view.
For each Bouncer Service S the project is transformed in memory (docker bouncer config prints the result; the compose file is never rewritten):
| Derived service | Gets | Does not get |
|---|---|---|
S (the proxy) |
proxy_image, S's ports:, expose:, networks with their aliases, logging, pull_policy, profiles; restart: unless-stopped; a health check on Envoy's /ready; the generated Envoy config |
S's image, environment, volumes, labels, depends_on and everything else |
S-app (the replicas) |
everything else from S: image, command, environment, env_file, volumes, labels, healthcheck, depends_on, restart, resources, expose:
|
ports:, network aliases, x-* extensions |
Consequences:
- Clients reach
Sexactly as they did under plain Compose: the same name, ports and aliases now resolve to Envoy. - Another service's
depends_on: { S: { condition: service_healthy } }waits for the proxy to be ready, not for the replicas. - Labels you put on
S(for example for a reverse proxy in front) land on the replicas, not the proxy. - Containers are named as Compose names them: the proxy
<project>-S-1, the replicas<project>-S-app-<n>.
Every label Bouncer sets starts with dev.cuza.bouncer. (reverse DNS, like Compose's com.docker.compose.*).
| Label | On | Content |
|---|---|---|
dev.cuza.bouncer.managed |
proxy, replicas | true |
dev.cuza.bouncer.role |
proxy, replicas, lock |
proxy, replica or lock
|
dev.cuza.bouncer.service |
proxy, replicas | the Service name S
|
dev.cuza.bouncer.admin-port |
proxy | Envoy's admin port inside the container (9901, or 19901 if 9901 is a service port) |
dev.cuza.bouncer.revision |
replicas | per-Service revision number, increasing from 1 |
dev.cuza.bouncer.spec |
replicas | base64(gzip(JSON of the normalised S-app config)) |
dev.cuza.bouncer.spec-hash |
replicas | first 16 hex digits of the SHA-256 of that JSON; the change detector |
dev.cuza.bouncer.history |
replicas | base64(gzip(JSON list of the previous revisions, newest first, each {revision, time, up_id, spec, version} with the spec as plain JSON)), at most history_max entries and 64 KiB |
dev.cuza.bouncer.up-id |
replicas | ID of the up/undo run that created the revision (its UTC start time) |
dev.cuza.bouncer.time |
replicas | when the revision was created (RFC 3339, UTC) |
dev.cuza.bouncer.image |
replicas | the exact image the replicas' image reference named when the revision was made: repo@sha256:… or an image ID; part of the spec (see Image pinning) |
dev.cuza.bouncer.invocation |
replicas | base64(gzip(JSON)) of how the up or undo that made the revision loaded the project: compose files and project directory (absolute), project name, --env-file paths (absolute; never their contents), active profiles, the command (up or undo, plus the revision an undo restored) and the Bouncer version; what refresh replays |
dev.cuza.bouncer.version |
replicas, lock | the Bouncer version that created the container (dev for a local build); informational, never compared |
dev.cuza.bouncer.format |
replicas, lock | the storage format, MAJOR.MINOR (see Storage format) |
dev.cuza.bouncer.lock-owner |
lock |
user@host pid N of the run holding the lock |
The version and format labels are never on the proxy: they would change its Compose config hash, and a CLI upgrade alone would recreate it.
Like Helm keeps each release in a Secret, Bouncer keeps each revision of a Service in its replicas' labels. When up finds that the file's replica config hashes differently from the running replicas' dev.cuza.bouncer.spec-hash, it stamps the new replicas with:
- the next revision number,
- the normalised config (
dev.cuza.bouncer.spec), - the previous revision plus its history, trimmed to
history_maxand to a 64 KiB label (dev.cuza.bouncer.history).
The whole history is one gzip stream of plain JSON specs, so consecutive revisions that differ in one field compress against each other: 100 revisions where only the image digest changes take about 5 KiB. If the encoded history would exceed 64 KiB, the oldest revisions are dropped until it fits and up/undo warn History trimmed: to N revisions (64 KiB label budget).
Every replica of a revision carries the full record, so any running replica can answer history and undo, and nothing outside Docker needs backing up. The flip side: down deletes the history with the containers.
Normalising removes what should not trigger a bounce: ports:, network aliases, x-* extensions, the replica count (deploy.replicas/scale), Bouncer's own bookkeeping labels (revision, invocation, version, format), and every key loaded from an env_file. Every other field, including your labels and dev.cuza.bouncer.image, is part of the spec.
A tag such as :latest can name a new image without the compose file changing, and a rebuild makes a new image under the same <project>-<service> name. So before hashing, up resolves the replicas' image reference against the local image and records the exact image in the dev.cuza.bouncer.image label:
-
repo@sha256:<digest>when the local image has a registry digest for the reference's repository (it was pulled from there, so it can be pulled again by digest); - otherwise the image ID (
sha256:…): a built image, or one only tagged locally; - a reference that already carries
@sha256:…is exact as written.
The replicas keep the image reference as you wrote it (registry/app:v1, <project>-<service>), so docker ps, docker bouncer ps and history stay readable; history adds the short digest or ID, as in registry/app:v1 (1a2b3c4d5e6f). A moved tag or a rebuild with a new ID changes the label, so the spec hash, so the Service bounces; the same image is a no-op. undo runs a restored revision's exact image (see Operations). ps resolves the file's image the same way to compare it with the running replicas.
Upgrading to the docker-bouncer version that added this label bounces every Service once on its next up, since its spec gains the label, even though the image is unchanged.
Each key defined in a service's env_files is removed from the stored environment, whatever its value, and so is an explicit environment: entry with the same key. The stored spec keeps the env_file paths (and whether each is required). When undo restores a revision it reads those files again, resolving variables against the project environment as Compose does, so an undone replica gets the current secrets rather than ones frozen in a label.
Two consequences:
- Changing only a value inside an env file does not change the spec hash, so it does not trigger a bounce.
- Values written directly under
environment:(including ones interpolated from.envor the shell) are stored in the labels as they are. Keep secrets inenv_files.
refresh has no compose file or directory to go on, so every up and undo records on the replicas it creates how it loaded the project (dev.cuza.bouncer.invocation). An up that only scales records its own invocation on the replicas it adds (it is outside the spec, so no bounce). refresh reads it from the newest replica of the newest up-id and loads the project the same way, from any directory. Only paths are stored: env files are read again at each refresh, and variables the compose files took from the shell are not replayed (refresh warns about them). The label is bookkeeping, outside the spec, so an up from another directory never bounces. When the last revision of a Service came from undo, the recorded command is undo, and refresh holds the Service there until the next up.
Replicas and the lock carry a MAJOR.MINOR format for how Bouncer stores state (labels, spec, history and invocation encodings):
- MINOR bumps for additive changes older CLIs can safely ignore (a new optional label or field); any minor works with any CLI of the same major, and a CLI meeting a newer minor warns once.
- MAJOR bumps only when older CLIs would misread or corrupt state. A stack at a newer major makes
upandundorefuse with exit 2 andrefreshfail that project with exit 1;ps,history,logs,ls,stopanddownwork with a warning.--ignore-formatturns the refusal into a warning. - A release that drops reading an old major raises the minimum readable major; stacks below it are refused the same way, with a message to
downthem with their own version first. - Every format bump comes with a migration step (a pure function over the stored specs, history and invocation) and its test. Every read of stored payloads runs through the chain, so
undorestores old revisions;docker bouncer migratere-bounces a stack once so its replicas carry the CLI's format. A stack without the label (from before formats were recorded) reads as 1.0 and is migrated by a relabelling bounce.
The version label is informational: compatibility is decided by the format alone.
The proxy runs the stock Envoy image with its config passed inline through two environment variables, so nothing is written on the host:
-
BOUNCER_BOOTSTRAP, the static config: one dual-stack listener on[::]:<port>(IPv4 too;0.0.0.0on a kernel without IPv6) per distinct container port, each routing every request to a clusterport-<port>; the admin interface on127.0.0.1:<admin port>; clusters loaded from the file/etc/bouncer/dyn/cds.json, with its directory watched for changes. It never names a replica, so it changes only when the proxy's own definition does. -
BOUNCER_CDS, the seed cluster file. The entrypoint writes it only if the file does not exist yet, so a restarted proxy keeps the last list Bouncer wrote. The seed is the bare minimum a fresh proxy needs to reach the replicas: one cluster per port pointing at the replica service nameS-app, with DNS settings and the connect timeout, and no health checks. It depends only on the ports and the service name, so health or drain settings never change the proxy's definition.
Bouncer then keeps the live cluster file inside the container: real replica names plus the health check settings below. At the start of every run Bouncer reads that file and compares it with what the current config renders for the same replicas. If the settings differ (a changed healthcheck.uri, or a file written by an older docker-bouncer), it writes the file again with the same replicas and Envoy reloads it in place: no recreate, no traffic gap, and the Service row shows Proxy config updated. If nothing differs, nothing is written. A seed is replaced by the real replica list as soon as the planner sets the list.
Listener and route settings:
| Setting | Value | Why |
|---|---|---|
route timeout
|
0s (none) |
long requests are not cut by the proxy |
retry_on |
connect-failure,refused-stream,reset-before-request |
retries only requests that never reached the app, so they are safe to retry |
num_retries |
3, preferring a different replica each time |
a replica that just stopped is skipped |
validate_clusters |
false |
routes may name clusters before the cluster file defines them |
use_remote_address, xff_num_trusted_hops
|
true, 1
|
the caller's address is appended to X-Forwarded-For; an incoming X-Forwarded-Proto is kept (see Troubleshooting) |
preserve_external_request_id |
true |
an incoming x-request-id is kept |
upgrade_configs |
websocket plus upgrade_types
|
WebSocket and listed upgrades pass; Envoy answers 403 to any other Upgrade request |
stream_idle_timeout |
0s (none) |
an idle upgraded connection (a quiet WebSocket) is not cut after Envoy's default 5 minutes; the app's own timeouts govern, as with the route timeout
|
Cluster settings (one cluster per container port):
| Setting | Value |
|---|---|
type |
STRICT_DNS: endpoints are replica container names, re-resolved every 1s (IPv4 only), so a replica restarted with a new IP keeps receiving traffic |
connect_timeout |
1s |
| health check | HTTP GET <healthcheck.uri> on the first port (no uri: GET /, 200–499 pass; uri set: 2xx pass), every 1s, timeout 1s, unhealthy after 2 failures, healthy after 1 success; new hosts get no traffic until their first check passes |
The Envoy image has bash but no curl, so every call is a docker exec into the proxy:
-
Writing the list: the new cluster file is written to a temporary file and renamed over
cds.json, which Envoy picks up through the watched directory. Bouncer then polls the admin counterscluster_manager.cds.update_successandupdate_rejectedand returns only once Envoy applied the update (or fails if it was rejected or not applied within 10s). So a drain never starts before routing changed. -
Reading health:
GET /clusterson the admin port, spoken as HTTP/1.1 over bash's/dev/tcp(Envoy's admin answers HTTP/1.0 with426). A replica is healthy only if it is healthy in every cluster. -
Counting connections for
drain_method: envoy:cat /proc/net/tcp, countingESTABLISHEDsockets to the replica's IPs on the Service's ports. -
HTTP drain hooks: sent from the proxy to
<replica name>:<first port>the same way. - The proxy's own Docker health check sends
GET /readyto the admin port and expectsLIVE, every 5s.
Each Service's bounce is a loop: observe → plan → act, until done or failed. Services bounce in parallel, one loop each.
-
Observe: the Service's replicas from Docker (running, Docker health, created and started times, labels, IPs) and Envoy's per-replica health from
/clusters. The runner adds what it tracks during the run: since when each replica has been healthy, and the list it last wrote. -
Plan: a pure function,
bounce.Plan, returns one step from that snapshot. Replicas whosedev.cuza.bouncer.spec-hashequals the desired one are new, the rest old. Rules, first match wins:- A new replica past its health deadline and not healthy → fail.
- An old replica that is not running → drain it (remove at once).
- Envoy's list differs from the set of listable replicas (running, and Docker-healthy if they have a healthcheck) → set the list. This re-adds a half-drained replica after a killed run and adds new replicas as they start.
- No old replicas left: more than N new → drain the least healthy surplus one; fewer than N → start one; all N healthy → done; else wait.
- Capacity allows losing an old replica (see Bounce methods) → drain it, those not serving first, then oldest first.
- Fewer than N new and the surge allows → start one more.
- Otherwise → wait.
-
Act: start runs Compose for
S-appwith the new replica count, never recreating existing replicas; drain takes the replica off the list and applies the drain method, then stops and removes it through the Docker API; wait returns on the project's next container event (create, start, die, stop, destroy, health status) or after a second.
Because every pass starts from what is actually running, a run can be interrupted at any point and the next up carries on from there. The animations are produced by running this same bounce.Plan against a simulated world.
The four docs/bounce-<method>.svg files are generated from the planner:
go run ./docs/animationsRun it from the repository root after changing internal/bounce. It writes only those four files; docs/how-it-works.svg (the animation at the top of this page) is drawn by hand and left alone.