| 1 | # Containers |
| 2 | |
| 3 | A container runs a program that is not JavaScript beside the Worker that needs |
| 4 | it. A Durable Object starts the container, reaches it over a TCP port, and stops |
| 5 | it. The container disk is ephemeral, so durable state belongs in the storage of |
| 6 | the object or in another binding. Containers are experimental in celld, so the |
| 7 | configuration keys, the `ctx.container` surface, the node defaults, and the |
| 8 | security boundary can change without notice. Read the |
| 9 | [Cloudflare Containers documentation](https://developers.cloudflare.com/containers/) |
| 10 | for the standard API. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [Containers example](../../examples/container) starts a Python HTTP server |
| 15 | and reaches it through a container port. |
| 16 | |
| 17 | <!-- celld-example: container --> |
| 18 | |
| 19 | ## The object and its container |
| 20 | |
| 21 | A `containers` entry names a SQLite-backed Durable Object class of the same |
| 22 | script. Each object of that class gets a `ctx.container` handle and controls at |
| 23 | most one container. |
| 24 | |
| 25 | celld 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 | |
| 38 | On Linux, `getTcpPort(port)` reaches every port through the bridge address of |
| 39 | the container. A fetch with an `Upgrade: websocket` header gives a 101 response |
| 40 | with 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 |
| 45 | runs as published on the `cloudflare/sandbox` image, over the HTTP and the RPC |
| 46 | transports. Its preview URLs need a wildcard hostname at the load balancer, |
| 47 | tunnels run cloudflared inside the container, and bucket mounts use the |
| 48 | credentials 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, |
| 54 | with the `docker` CLI on `PATH` or the CLI that `CELLD_DOCKER` names. A Podman |
| 55 | CLI works. It saves the image to `deploy/images/<key>.tar` in the fleet bucket, |
| 56 | where the key hashes the image layers and config, so a redeploy of an unchanged |
| 57 | image uploads nothing. A node loads the image into its own engine the first time |
| 58 | a cell of the class starts, and never contacts a registry. The `celld-fence` |
| 59 | image 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 |
| 63 | local machine and keeps the image in the local engine. |
| 64 | |
| 65 | The container runs on the node that owns the cell. Each node that serves a |
| 66 | container class needs a Docker or Podman daemon. celld uses the Unix socket |
| 67 | that `DOCKER_HOST` names, or the default socket of Docker, Docker Desktop, |
| 68 | OrbStack, or Podman. A `DOCKER_HOST` value that is not a `unix://` URL gives |
| 69 | celld no socket. A node without an engine cannot activate a cell of a container |
| 70 | class, and it serves every other class. |
| 71 | |
| 72 | ## The isolation boundary |
| 73 | |
| 74 | Under the default runtime, a container shares the kernel of the node and is not |
| 75 | a virtual machine. celld drops all Linux capabilities, sets |
| 76 | `no-new-privileges`, keeps the default seccomp profile of the daemon, limits the |
| 77 | container to 1024 processes, and runs an init as PID 1 that reaps orphans. Two |
| 78 | containers on one node cannot connect to each other. |
| 79 | |
| 80 | Before the first container starts, the node installs nftables rules on its |
| 81 | container bridges through a one-shot privileged container of the `celld-fence` |
| 82 | image. A container with `enableInternet: true` reaches the Internet, but not |
| 83 | the node, another node, the private ranges `10/8`, `172.16/12`, `192.168/16`, |
| 84 | and `100.64/10`, or the link-local range. A node that cannot install the rules |
| 85 | starts no container, and a deployment from a celld without the fence image |
| 86 | starts no container on a node that has the fence. |
| 87 | |
| 88 | `enableInternet: false` attaches the container to an internal bridge with no |
| 89 | route out. On macOS, an internal network publishes no port, so `celld dev` on |
| 90 | macOS 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, |
| 93 | for example `runsc` for gVisor or `kata` for a virtual machine. A `runtime` key |
| 94 | in a `containers` entry overrides it for that class; Cloudflare has no such key. |
| 95 | If the daemon lacks the runtime, every `start()` fails with the daemon error. |
| 96 | For code that you did not write, name a runtime that gives each container its |
| 97 | own kernel, and keep the secrets in the Worker. |
| 98 | |
| 99 | A 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 |
| 101 | cannot reach the built-in resolver of the engine. The fence permits the public |
| 102 | resolver. A container on the default runtime keeps the engine resolver unless |
| 103 | the 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 |
| 108 | on Cloudflare. An idle eviction of the cell keeps the container running for that |
| 109 | window, and the next activation on the same node reconnects to it. A move to |
| 110 | another node, a node restart, a reset, and a node stop destroy the container. |
| 111 | Ctrl-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` |
| 114 | and its alias `standard`, `standard-2`, `standard-3`, and `standard-4`, with the |
| 115 | CPU and memory limits of |
| 116 | [the Cloudflare limits page](https://developers.cloudflare.com/containers/platform/limits/). |
| 117 | The default is `dev`: a sixteenth of a CPU and 256 MiB, as on Cloudflare. Name a |
| 118 | larger type when the image boots an interpreter. |
| 119 | |
| 120 | The node counts the memory cap of every running container as committed memory, |
| 121 | because the container cgroup is outside the node process. A container-heavy |
| 122 | node therefore reports no headroom and sheds cells. Set the node memory ceiling |
| 123 | at or above the largest instance type in use, plus headroom. |
| 124 | |
| 125 | Each node publishes its running container count per class in the shared |
| 126 | capacity sample. Before a start, a node sums the fleet count and its own live |
| 127 | count 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 | |
| 159 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists |
| 160 | the runtime APIs and the unsupported services. |