Diffinery

homeflare/celldpublic

celld/docs/services/containers.md
215 lines · 12.5 KBRaw History
1# Containers
2
3A container runs a program that is not JavaScript beside the Worker that needs
4it. A Durable Object supervises that program, so the object starts the
5container, reaches it over a TCP port, and stops it. An application uses a
6container for a language or a dependency that the Workers runtime does not
7have, for a heavy computation, or for code that a customer supplies. The
8container disk is ephemeral, therefore durable state belongs in the object's
9storage or in another binding. Containers are experimental in celld, so the
10configuration keys, the `ctx.container` surface, the node-side defaults, and the
11security boundary can change without notice. Read the
12[Cloudflare Containers documentation](https://developers.cloudflare.com/containers/)
13for the standard API behavior.
14
15## Example
16
17The [Containers example](../../examples/container) starts a Python HTTP server
18and reaches it through a container port.
19
20<!-- celld-example: container -->
21
22## The object and its container
23
24A `containers` entry names a Durable Object class, and each object of that class
25controls at most one container. The class must be SQLite-backed, and it must
26belong to the same script. celld gives such an object a `ctx.container` handle,
27and an object of any other class has none. A Worker therefore addresses a
28container the way it addresses an object, with a name or an id.
29
30celld implements `running`, `start()`, `monitor()`, `destroy()`, `signal()`,
31`getTcpPort()`, `exec()`, and `setInactivityTimeout()`. `start()` accepts
32`entrypoint`, `env`, `enableInternet`, and `labels`. It checks `hardTimeout`
33and then ignores it, so a value of 0 or less throws and a valid value changes
34nothing. `inspect()`, the snapshot methods, and the outbound interception
35methods reject with an error. Read the
36[`ctx.container` reference](https://developers.cloudflare.com/durable-objects/api/container/)
37for the standard method list.
38
39A request reaches the container through a port, and not through a public
40address. `getTcpPort(port).fetch()` opens a connection from the node to the
41container and speaks HTTP on it. On Linux, the node uses the container's bridge
42address, so every port works. On macOS, the node uses a published loopback port,
43so the image must declare the port with `EXPOSE`, as `wrangler dev` requires. A
44fetch with an `Upgrade: websocket` header gives a 101 response with a
45`webSocket`, as on Cloudflare. `getTcpPort(port).connect()` gives a raw socket
46with the lifetime of the event that opened it. See
47[TCP sockets](../cloudflare-compat.md#tcp-sockets).
48
49A `monitor()` promise and an `exec()` process have that same event lifetime.
50They settle only inside the event that created them, and after the handler
51answers they do not keep the object active, so a drain can move the object.
52`running` always reports the engine's state, therefore an object that lost its
53`monitor()` sees the exit at its next event. A `start()` that fails is logged by
54the node as `container_start_failed`.
55
56`@cloudflare/containers` runs as published, and the example above uses it. Its
57`sleepAfter` alarm stops the container, and `getContainer()`, `getRandom()`, and
58`switchPort()` work. Read the
59[Container class reference](https://developers.cloudflare.com/containers/reference/container-class/)
60for the hooks and the defaults. `@cloudflare/sandbox` also runs as published, on
61the `cloudflare/sandbox` image. Its `exec()` API, files, processes, sessions,
62and code interpreter run inside the container, over the HTTP transport and over
63the RPC transport. Preview URLs need a wildcard hostname at the operator's load
64balancer, tunnels run cloudflared inside the container, and bucket mounts use
65the credentials that the application passes. See the
66[sandbox example](../../examples/sandbox).
67
68## The image and the node
69
70Cloudflare pushes an image to its own registry, distributes it across its
71network, and starts the container in a virtual machine at
72[the nearest location that already holds the image](https://developers.cloudflare.com/containers/concepts/architecture/).
73An object and its container can therefore run in different places. celld has no
74registry and no global network, so it builds the same two steps out of the fleet
75bucket and the node's own container engine.
76
77`celld deploy` builds a Dockerfile in the project, or it pulls an image
78reference, with the `docker` CLI on `PATH` or with the CLI that `CELLD_DOCKER`
79names. A Podman CLI works. `celld deploy` then saves the image to the fleet
80bucket at `deploy/images/<key>.tar`, where the key hashes the image layers and
81the image config. A node loads the image into its own engine the first time a
82cell of the class starts, so a node never contacts a registry, and a redeploy of
83an unchanged image uploads nothing. The `celld-fence` image that a node needs
84rides with every deployment that has a container.
85
86`celld deploy` builds and pulls for `linux/amd64`, as Wrangler does, so a deploy
87from an ARM machine runs on x86-64 nodes. `CELLD_CONTAINER_PLATFORM` selects
88another platform. `celld dev` builds for the local machine and keeps the image
89in the local engine.
90
91The container runs on the node that owns the cell, and the two never separate.
92Each node that serves a container class therefore needs a Docker daemon or a
93Podman daemon. celld uses the Unix socket that `DOCKER_HOST` names, or the
94default socket of Docker, Docker Desktop, OrbStack, or Podman. A `DOCKER_HOST`
95value that is not a `unix://` URL gives celld no socket at all. A node without
96an engine cannot activate a cell of a container class, and it serves every other
97class.
98
99![A Worker on any node is routed to the node that owns the cell, where a V8 isolate and a container process run on two sides of the container engine socket, behind an nftables fence on the node's bridge, and a move destroys the container](containers-flow.svg)
100
101## The isolation boundary
102
103Cloudflare runs each container in its own virtual machine, so Cloudflare
104publishes no capability policy. Under the default runtime a celld container
105shares the node's kernel, so celld sets a policy of its own. celld drops all
106Linux capabilities, sets `no-new-privileges`, keeps the daemon's default seccomp
107profile, limits the container to 1024 processes, and runs an init as PID 1 that
108reaps an orphaned process. Two containers on one node cannot open connections to
109each other.
110
111The node fences its container bridges before the first container starts. The
112rules are nftables rules on the node, outside every container and every runtime,
113and celld installs them through the daemon with a one-shot privileged container
114of the `celld-fence` image. A container with `enableInternet: true` reaches the
115Internet and nothing of the node's own. It cannot open a connection to the node,
116to another node, to the private ranges `10/8`, `172.16/12`, `192.168/16`, and
117`100.64/10`, or to the link-local range, so the node's internal listener and a
118cloud metadata service are both out of reach. A node that cannot install the
119rules starts no container, and a deployment from a celld without the fence image
120starts no container on a node that has the fence.
121
122`enableInternet: false` attaches the container to an internal bridge with no
123route out. On macOS, the node reaches a container only through a published port,
124and an internal network publishes none, so `celld dev` on macOS keeps egress on
125and logs a warning. The fence still applies.
126
127`CELLD_CONTAINER_RUNTIME` names the OCI runtime for every container on the node,
128for example `runsc` for gVisor or `kata` for a virtual machine. A `containers`
129entry can also name a `runtime` of its own, which overrides the node setting for
130that class. celld adds this key, and the Cloudflare configuration has no
131equivalent. The daemon must have the named runtime, or every `start()` of such a
132container fails with the daemon's error, so a class runs only where its
133isolation exists. Under the default runtime, a container is a kernel namespace
134boundary and not a virtual machine. For code that you did not write, name a
135runtime that gives each container its own kernel, and keep the secrets in the
136Worker.
137
138A container that runs under a named runtime gets a written `/etc/resolv.conf`
139with public resolvers, `1.1.1.1` and `1.0.0.1` by default, or the
140`CELLD_CONTAINER_DNS` resolvers when the operator sets them. gVisor cannot reach
141the container engine's built-in resolver, so a container under gVisor resolves
142no hostname otherwise, and the fence permits the public resolver. A container on
143the node's default runtime keeps the engine's resolver, unless the operator sets
144`CELLD_CONTAINER_DNS`.
145
146## Sleep, wake, and the node's resources
147
148Cloudflare stops an idle container after 10 minutes, and `sleepAfter` changes
149that window. celld keeps the same default and the same mechanism.
150`setInactivityTimeout()` sets the window for the engine, and celld uses 10
151minutes without one. An idle eviction of the cell keeps the container running
152for that window, and the next activation of the cell on the same node reconnects
153to it. A move to another node, a node restart, and a reset destroy the container
154instead, so the container disk is ephemeral, as on Cloudflare. A node stop
155destroys every container of the node, and Ctrl-C on `celld dev` does the same
156while it keeps the local state.
157
158`instance_type` sets the CPU limit and the memory limit of the Cloudflare
159instance type with that name. celld accepts `lite` and its older name `dev`,
160`basic`, `standard-1` and its alias `standard`, `standard-2`, `standard-3`, and
161`standard-4`, with the values that
162[the Cloudflare limits page](https://developers.cloudflare.com/containers/platform/limits/)
163gives. An entry without an `instance_type` gets `dev`, a sixteenth of a CPU and
164256 MiB, as on Cloudflare. A sixteenth of a CPU is a slow start for an
165interpreter, so name a larger type when the image boots a runtime. celld does
166not enforce the disk size of the type.
167
168A container runs in its own cgroup, so the node process does not carry the
169container's memory in its own charge. The node therefore counts the instance
170type's memory cap of every running container as committed memory. A
171container-heavy node reports no headroom and sheds cells, which keeps the node
172away from the kernel out-of-memory killer. An operator must set the node memory
173ceiling at or above the largest instance type in use, plus headroom.
174
175`max_instances` caps the containers that one class runs across the fleet.
176Cloudflare holds that cap centrally. celld has no coordinator, so each node
177publishes its running container count per class in the shared capacity sample. A
178node that is about to start a container sums that count across the fleet and
179adds its own live count. A start over the cap fails, and the node logs
180`container_start_failed` with the limit. The sample can lag one refresh,
181therefore two nodes that start the same class at once can exceed the cap for a
182moment and converge at the next refresh. `celld dev` reads no sample, so it
183enforces no fleet cap.
184
185## Differences from Cloudflare
186
187- A `containers` entry accepts `class_name`, `image`, `name`, `instance_type`,
188 `max_instances`, and `runtime`. Each other key stops the deployment. celld
189 adds `runtime`, and it accepts `name` without using it.
190- celld places a container on the node that owns the cell. Cloudflare can place
191 a container away from its object.
192- A node that serves a container class needs a Docker daemon or a Podman daemon.
193- celld does not enforce the disk size of an instance type.
194- `max_instances` converges across the fleet instead of holding centrally, so
195 the fleet can exceed the cap for one refresh. `celld dev` enforces no cap.
196- `inspect()`, `snapshotDirectory()`, `snapshotContainer()`, and the outbound
197 interception methods reject with an error. `start()` checks `hardTimeout` and
198 then ignores it, so a value of 0 or less throws and a valid value has no
199 effect.
200- `getTcpPort(port).connect()` gives a socket with the lifetime of the event
201 that opened it. See [TCP sockets](../cloudflare-compat.md#tcp-sockets).
202- A `monitor()` promise and an `exec()` process do not keep the object active
203 after the handler answers.
204- On macOS, the node reaches a container through a published port, so the image
205 must declare the port with `EXPOSE`. `enableInternet: false` has no effect on
206 macOS.
207- The container bridges carry no IPv6 address, and the fence rejects the IPv6
208 link-local and unique-local ranges as well.
209- A move of an object to another node stops its container, so the first
210 `@cloudflare/sandbox` call after the move can fail with the SDK's
211 `OperationInterruptedError`, as after a container restart on Cloudflare. The
212 next call starts a fresh container.
213
214The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists
215the runtime APIs and the unsupported services.