Diffinery

homeflare/celldpublic

celld/docs/services/containers.md
160 lines · 8.4 KBRaw History
1# Containers
2
3A container runs a program that is not JavaScript beside the Worker that needs
4it. A Durable Object starts the container, reaches it over a TCP port, and stops
5it. The container disk is ephemeral, so durable state belongs in the storage of
6the object or in another binding. Containers are experimental in celld, so the
7configuration keys, the `ctx.container` surface, the node defaults, and the
8security boundary can change without notice. Read the
9[Cloudflare Containers documentation](https://developers.cloudflare.com/containers/)
10for the standard API.
11
12## Example
13
14The [Containers example](../../examples/container) starts a Python HTTP server
15and reaches it through a container port.
16
17<!-- celld-example: container -->
18
19## The object and its container
20
21A `containers` entry names a SQLite-backed Durable Object class of the same
22script. Each object of that class gets a `ctx.container` handle and controls at
23most one container.
24
25celld implements this part of `ctx.container`:
26
27- `start()` starts the container. It accepts `entrypoint`, `env`,
28 `enableInternet`, and `labels`.
29- `running` reports the state of the engine, so an object that lost its
30 `monitor()` sees the exit at its next event.
31- `monitor()` returns a promise that settles when the container exits.
32- `destroy()` stops the container, and `signal()` sends it a signal.
33- `getTcpPort(port).fetch()` sends an HTTP request to a port, and
34 `getTcpPort(port).connect()` opens a raw socket.
35- `exec()` runs a process in the container.
36- `setInactivityTimeout()` sets the idle window.
37
38On Linux, `getTcpPort(port)` reaches every port through the bridge address of
39the container. A fetch with an `Upgrade: websocket` header gives a 101 response
40with a `webSocket`, as on Cloudflare. The node logs a failed `start()` as
41`container_start_failed`.
42
43`@cloudflare/containers` runs as published, including the `sleepAfter` alarm,
44`getContainer()`, `getRandom()`, and `switchPort()`. `@cloudflare/sandbox` also
45runs as published on the `cloudflare/sandbox` image, over the HTTP and the RPC
46transports. Its preview URLs need a wildcard hostname at the load balancer,
47tunnels run cloudflared inside the container, and bucket mounts use the
48credentials that the application passes. See the
49[sandbox example](../../examples/sandbox).
50
51## The image and the node
52
53`celld deploy` builds a Dockerfile in the project, or pulls an image reference,
54with the `docker` CLI on `PATH` or the CLI that `CELLD_DOCKER` names. A Podman
55CLI works. It saves the image to `deploy/images/<key>.tar` in the fleet bucket,
56where the key hashes the image layers and config, so a redeploy of an unchanged
57image uploads nothing. A node loads the image into its own engine the first time
58a cell of the class starts, and never contacts a registry. The `celld-fence`
59image rides with every deployment that has a container.
60
61`celld deploy` builds and pulls for `linux/amd64`, as Wrangler does.
62`CELLD_CONTAINER_PLATFORM` selects another platform. `celld dev` builds for the
63local machine and keeps the image in the local engine.
64
65The container runs on the node that owns the cell. Each node that serves a
66container class needs a Docker or Podman daemon. celld uses the Unix socket
67that `DOCKER_HOST` names, or the default socket of Docker, Docker Desktop,
68OrbStack, or Podman. A `DOCKER_HOST` value that is not a `unix://` URL gives
69celld no socket. A node without an engine cannot activate a cell of a container
70class, and it serves every other class.
71
72## The isolation boundary
73
74Under the default runtime, a container shares the kernel of the node and is not
75a virtual machine. celld drops all Linux capabilities, sets
76`no-new-privileges`, keeps the default seccomp profile of the daemon, limits the
77container to 1024 processes, and runs an init as PID 1 that reaps orphans. Two
78containers on one node cannot connect to each other.
79
80Before the first container starts, the node installs nftables rules on its
81container bridges through a one-shot privileged container of the `celld-fence`
82image. A container with `enableInternet: true` reaches the Internet, but not
83the node, another node, the private ranges `10/8`, `172.16/12`, `192.168/16`,
84and `100.64/10`, or the link-local range. A node that cannot install the rules
85starts no container, and a deployment from a celld without the fence image
86starts no container on a node that has the fence.
87
88`enableInternet: false` attaches the container to an internal bridge with no
89route out. On macOS, an internal network publishes no port, so `celld dev` on
90macOS keeps egress on and logs a warning. The fence still applies.
91
92`CELLD_CONTAINER_RUNTIME` names the OCI runtime for every container on the node,
93for example `runsc` for gVisor or `kata` for a virtual machine. A `runtime` key
94in a `containers` entry overrides it for that class; Cloudflare has no such key.
95If the daemon lacks the runtime, every `start()` fails with the daemon error.
96For code that you did not write, name a runtime that gives each container its
97own kernel, and keep the secrets in the Worker.
98
99A container under a named runtime gets a written `/etc/resolv.conf` with
100`1.1.1.1` and `1.0.0.1`, or the `CELLD_CONTAINER_DNS` resolvers, because gVisor
101cannot reach the built-in resolver of the engine. The fence permits the public
102resolver. A container on the default runtime keeps the engine resolver unless
103the operator sets `CELLD_CONTAINER_DNS`.
104
105## Sleep, wake, and the node's resources
106
107`setInactivityTimeout()` sets the idle window, and the default is 10 minutes, as
108on Cloudflare. An idle eviction of the cell keeps the container running for that
109window, and the next activation on the same node reconnects to it. A move to
110another node, a node restart, a reset, and a node stop destroy the container.
111Ctrl-C on `celld dev` destroys the containers and keeps the local state.
112
113`instance_type` accepts `lite` and its older name `dev`, `basic`, `standard-1`
114and its alias `standard`, `standard-2`, `standard-3`, and `standard-4`, with the
115CPU and memory limits of
116[the Cloudflare limits page](https://developers.cloudflare.com/containers/platform/limits/).
117The default is `dev`: a sixteenth of a CPU and 256 MiB, as on Cloudflare. Name a
118larger type when the image boots an interpreter.
119
120The node counts the memory cap of every running container as committed memory,
121because the container cgroup is outside the node process. A container-heavy
122node therefore reports no headroom and sheds cells. Set the node memory ceiling
123at or above the largest instance type in use, plus headroom.
124
125Each node publishes its running container count per class in the shared
126capacity sample. Before a start, a node sums the fleet count and its own live
127count against `max_instances`. A start over the cap fails, and the node logs
128`container_start_failed` with the limit.
129
130## Differences from Cloudflare
131
132- A `containers` entry accepts `class_name`, `image`, `name`, `instance_type`,
133 `max_instances`, and `runtime`. Each other key stops the deployment. celld
134 adds `runtime`, and it accepts `name` without using it.
135- celld places a container on the node that owns the cell. Cloudflare can place
136 a container away from its object.
137- A node that serves a container class needs a Docker daemon or a Podman daemon.
138- celld does not enforce the disk size of an instance type.
139- `max_instances` converges across the fleet instead of holding centrally, so
140 the fleet can exceed the cap for one refresh. `celld dev` enforces no cap.
141- `inspect()`, `snapshotDirectory()`, `snapshotContainer()`, and the outbound
142 interception methods reject with an error. `start()` checks `hardTimeout` and
143 then ignores it, so a value of 0 or less throws and a valid value has no
144 effect.
145- `getTcpPort(port).connect()` gives a socket with the lifetime of the event
146 that opened it. See [TCP sockets](../cloudflare-compat.md#tcp-sockets).
147- A `monitor()` promise and an `exec()` process do not keep the object active
148 after the handler answers.
149- On macOS, the node reaches a container through a published port, so the image
150 must declare the port with `EXPOSE`. `enableInternet: false` has no effect on
151 macOS.
152- The container bridges carry no IPv6 address, and the fence rejects the IPv6
153 link-local and unique-local ranges as well.
154- A move of an object to another node stops its container, so the first
155 `@cloudflare/sandbox` call after the move can fail with the SDK's
156 `OperationInterruptedError`, as after a container restart on Cloudflare. The
157 next call starts a fresh container.
158
159The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists
160the runtime APIs and the unsupported services.