0012 — Docker client and agent op protocol v1
Status: accepted 2026-11-27, after security approval in step 15 of plan 0004 (systems reviewed every M3a step) · Date: 2026-11-21 (proposed) · 2026-11-27 (accepted)
Context. M3a is the first milestone that changes Docker state: it pulls images, creates, starts, stops and removes containers, follows Docker events, and reloads the embedded Caddy with app routes. Plan 0002 reserved an ADR for the Docker client and required a versioned op protocol between the control plane and the agent, with size caps and agent output treated as untrusted. M1 built a small typed client (internal/agent/dockerapi) with per-call deadlines and API negotiation (Engine 25+, API 1.44–1.52), and an op table in which the zero value is local-only (ADR 0009). Remote agents are logs-only until Phase 2 (Q1).
Decision.
- Keep the hand-rolled, typed Docker client. No Docker SDK module: it would add a large dependency tree, package
initwork in every mode (S4), and API churn we don't use. Every endpoint is a typed method with a fixed path; every request goes throughcall(a per-call deadline) orstream(bounded by its context, with a header deadline). Request bodies are Go types that contain only the fields ServerDock is allowed to set, and the final create body passes a default-deny check down to every leaf, in the control plane and again in the agent on the exact bytes sent to Docker. Docker's JSON lines are read with a 64 KiB per-line cap. - Ownership by label, enforced in the agent. Every container ServerDock creates carries
serverdock.managed,serverdock.instance(a random id per data directory, set by the agent itself),serverdock.project,serverdock.service,serverdock.deployandserverdock.attempt, runs a pinned image id, and is namedsd-<project>-<service>-d<deploy>-a<attempt>with ids that are never reused. The agent stops, restarts or removes only containers with its own instance label, addressed by a full id it inspected. Images that carry anyserverdock.*label are refused. - One app network per install with static addresses. A bridge network named after the install id, created with a subnet ServerDock chose, labelled, with inter-container traffic disabled in M3a. Every container gets a static IPv4 recorded with its deploy, so an upstream never moves to another container after a container or daemon restart. ServerDock publishes no host ports for apps; Caddy is the only entry point.
- Op protocol v1. One yamux stream per op. The hub writes one request line (at most 64 KiB)
{"v":1,"op":…,"args":…}; the agent answers with progress or event lines (at most 1 KiB each) and exactly one result line (at most the op's limit). Both sides refuse trailing bytes and unknown fields. The hub enforces per-op limits on line size, line count and total bytes, and cleans every string that came from Docker, a registry, an image or a container. A violation fails the op and closes the stream. Ahelloop (read-only, allowed on remote sessions, result at most 256 bytes) carries the protocol version; an agent that does not know it is treated as v0 (logsonly). The v0logsop is unchanged apart from an optionalfollowthat is never sent to a v0 agent.hellois frozen forever: its envelope is always"v":1, its args{"hub_max_v"}and its result{"v","agent"}, whatever version a session negotiates, so a later v2 hub and a v1 agent can still negotiate; other ops carry the session's negotiated version and the agent accepts1 <= v <= its maximum. Objects are decoded with exact key matching and no duplicate keys (encoding/jsonalone matches case-insensitively and keeps the last duplicate); the step-5 create-body policy must use the same rule. Event lines are exempt from the 4 MiB per-op cap (each is at most 1 KiB andeventsis local-only). The in-process local agent is never sent ahello(it is this binary). Text the hub stores or shows is cleaned; op payloads stay raw until the op's typed decoder cleans them. - All new ops are local-only, on both sides. The hub refuses them to any session other than the in-process local agent, and
serverdock agent(remote mode) does not serve them. The remote allowlist ishelloandlogs; the negotiated version never widens it. - The control plane owns the events reconnect. It keeps one
eventsop open on the local session (no op timeout), reconnects with a 1 s → 30 s jittered back-off while the stream is down, and on every (re)connect opens the stream first, then lists ServerDock's containers and re-inspects those with events buffered during the list. - The commit is authoritative after a crash. A deploy interrupted by a restart after it set the service's active deploy is finished (the old version is removed), not rolled back; an uncommitted one is marked failed and its containers removed. The boot pass changes only database state; one resync rule set, run after every successful events connection, does all container clean-up. This amends plan 0002's M3a interrupt rule.
Consequences.
- No new Go dependency; later milestones add endpoints (networks and volumes in M3b, image load in M5) under the same rules.
- A control-plane bug cannot delete containers that ServerDock did not create, two installs on one Docker host don't interfere, and a new service can never adopt an old service's container.
- ServerDock must reach the app network: it runs on the host or with host networking until the install shape (Q4) decides the container form. The app subnet is chosen at first start and not changed later.
- The per-op limits are part of the protocol; raising one is a protocol change reviewed by
security. - Idle cost: one events stream (one Docker connection and two goroutines); no timers of ours while the stream is up.
Addendum A (2026-11-29, plan 0005 step 2): networks, volumes and registry credentials for M3b
Accepted 2026-11-29, after security and systems approved plan 0005 step 2 (plan 0005 Revision 3). Decisions 1, 2 and 4 to 7 stand. Step 2 adds only client methods; the ops that use them come in step 3.
Decision.
- Decision 3 gains "plus one internal network per project for service DNS, joined at create time". Each project gets one more bridge network,
serverdock-<install id[:8]>-p<project id>, withInternal: true(no outside route, no NAT), its own /25 inside10.250.0.0/16and the labelsserverdock.managed,serverdock.instanceandserverdock.project. A container joins it in the create call itself, as a second endpoint carrying one alias, its service name (the Engine accepts several endpoints at create from API 1.44, our minimum). It stays on the app network too, with its static address. Joining at create time, not after the health gate, is what lets a new version reachdbby name while it is checked (plan 0005, Revision 1, B1). The cost is stated in the plan: during a blue-green deploy the name can answer from the new version before the old one stops. The client refuses (ErrNetworkSettings) any existing app or project network whosegateway_mode_ipv4or_ipv6is anything other than absent ornat:nat-unprotectedwould bring back direct routing to unpublished ports. - New ops, all
LocalOnlyand v1, with the rules of decision 4 (step 3 builds them):volume_ensure,volume_remove,project_network_ensureandnetwork_remove.- Volumes are bound to their service id. A volume is named
sd-<install id[:8]>-s<service id>-<name>, uses thelocaldriver with no options, and carriesserverdock.managed,serverdock.instanceandserverdock.service.volume_ensurerefuses a volume that exists with another driver, any option, or another instance or service id.volume_removeacts only on a volume with our instance label and aserverdock.serviceequal to theserviceargument, is never forced, and reports "in use" as a conflict. A container's create check ties the volume'ss<id>to the container's ownserverdock.servicelabel. - Project networks are created and removed only by name with our labels;
network_removeremoves only our labelled project networks, never forced, and goes by the full id after an inspect (Docker also matches id prefixes).
- Volumes are bound to their service id. A volume is named
- Registry credentials travel in one op argument and are never stored on the host.
image_pullgains an optionalauth(at most 4 KiB, base64url) that the agent puts in theX-Registry-Authheader of that one pull request. It is never in the URL or the body, never logged, never in a result, and it is removed by value (the header, its decoded JSON and the password inside) from every error text. There is nodocker loginand no credential helper, so nothing is left in a Docker config file; the next pull sends a credential again, or none. CreateSpecstays a typed whitelist. It gainsEnv,HostConfig.Mounts(volume and bind,rprivatebind propagation only),Memory,MemorySwap,NanoCpus,SecurityOptand theAliasesof an endpoint, and nothing else. A spec that does not use them marshals byte for byte as in M3a. The default-deny create check (step 4) still refuses every field it does not list. Env values of 4 bytes or more never appear in a create error, and none appears in a log line or a result; secrets are at least 8 bytes by input rule.
Consequences.
- No new Go dependency. The Engine endpoints used are
POST /volumes/create,GETandDELETE /volumes/{name},DELETE /networks/{id}and the existing network, image and container calls. - A project's services can reach each other on any port. Services of different projects share no network that allows container-to-container traffic (the app network keeps
enable_icc=false). Recorded on Docker 29.10.0 (plan 0005 step 2): a container on an internal network can open a TCP connection to a host listener bound to all addresses, through the network's gateway address, and has no default route, so it cannot reach an outside address. That is no host reach beyond what B10 already accepts for the app network's gateway. The real risk it names is third-party host listeners on0.0.0.0, such as a Docker TCP API, which any container of any project can then reach. Projects are isolated from each other and from the outside, not from the host. Also recorded: project A's container could not reach project B's container at either of B's addresses. - On a host whose resolv.conf has a search domain, a bare service name on an internal network can fail (Docker's resolver has no upstream there and answers the search-qualified name with SERVFAIL;
--dns-search .avoids it). The step 9 design must not assume a bare name resolves. - A new network is created with the
natgateway mode set explicitly on API 1.48 and later, so a daemon-widedefault-network-optscannot give it another gateway mode. The refusal of any other mode is short and points to the recovery steps indocs/architecture.md#gateway-mode-recovery. - Network options are default-deny, on the app network and on project networks (plan 0005 step 3). Only the gateway modes,
enable_icc,enable_ipv4,enable_ipv6and acom.docker.network.driver.mtuof 68 to 65535 are allowed. Any other option (a daemon-widedefault-network-optsentry, or one set by hand) refuses the network with a short message that points todocs/architecture.md#network-option-recovery. So an option indefault-network-optscan make a fresh install refuse its network; to keep recovery short, a network that the same call has just created is removed again (best effort, never forced) when its check fails, and the recovery is then to fixdaemon.jsonand deploy again. A network that already existed is never changed or removed by ServerDock. - A network cannot be removed or recreated under a stopped container: after the network is recreated under the same name, starting that container fails (its endpoint names the old network id). Step 9 recreates the container, not only the network.
- Docker clears
State.OOMKilledwhen a container restarts, so an OOM kill is recorded from theoomevent and never recovered from inspect. - A stopped container keeps its endpoints, address and alias on both networks, so a stop-first restore of the old version keeps its project name.
- The ownership rules grow with the resources: a volume or project network that lacks our labels is never touched, and a volume's service binding is checked in the agent, not trusted from the hub.