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