Durable Object Containers: API, Scheduling Policy, Lifecycle, and Deploy
How Cloudflare Containers run with Workers, which API and scheduling policy to choose, and how lifecycle, routing, images, and rollouts behave.
This page summarizes Cloudflare Containers documentation: Linux container images that run on Cloudflare’s network next to a Worker, with every instance reached only through a linked Durable Object. If you are choosing between two control surfaces, two scheduling policies, or wondering why the first request is slow, the sections below follow the same terms the docs use.
How a Container runs beside a Worker
Section titled “How a Container runs beside a Worker”A Cloudflare Container runs your image in a full Linux environment while the Worker handles inbound HTTP. The container is not exposed directly to end users; traffic always goes through the Worker and the Durable Object layer that owns that instance.
A Cloudflare Container runs your container image in a full Linux environment on Cloudflare’s network, alongside a Worker. The Worker handles incoming HTTP requests, while the Container provides the runtime, binaries, and languages your application needs.
— Cloudflare, Container Fundamentals
Each running instance lives in its own Firecracker microVM. Images should target linux/amd64. Treat the container’s local disk as ephemeral; persist data with Durable Object storage or bindings such as KV, R2, and D1, as described in Container Fundamentals and Lifecycle of a Container.
Containers APIs: ctx.container vs the Container class
Section titled “Containers APIs: ctx.container vs the Container class”Cloudflare documents two APIs that control the same container runtime. New applications should prefer the Durable Object Container API; the Container class from @cloudflare/containers remains for existing apps that rely on its helpers.
For new applications, we recommend the Durable Object Container API. It lets you combine container workloads with the Durable Object’s persistent storage, alarms, and request handling. Use ctx.container to control the container lifecycle while your Durable Object coordinates application state.
— Cloudflare, Containers API
| Requirement | Durable Object Container API | Container class |
|---|---|---|
| Start and stop | start(), signal(), destroy() |
start(), stop(), destroy() |
| Proxy traffic | getTcpPort(port).fetch() / connect() |
fetch(), containerFetch() |
| Port readiness | Application checks (e.g. health on getTcpPort()) |
startAndWaitForPorts(), waitForPort() |
| Idle shutdown | setInactivityTimeout() |
sleepAfter, onActivityExpired() |
| Lifecycle observation | monitor() |
onStart(), onStop(), onError() |
| Scheduling | Works with durable_object and default |
Only default scheduling policy |
The Container class does not support applications that use the durable_object scheduling policy. It is available only with the default scheduling policy. For durable_object applications, use the Durable Object Container API.
— Cloudflare, Container class
Starting a container does not mean its ports accept traffic yet. With the Durable Object Container API, ctx.container.running can be true before the process is ready; check port readiness before the first request, as in Lifecycle of a Container and Durable Object Container API.
When to migrate off the Container class
Section titled “When to migrate off the Container class”Migrate to the Durable Object Container API keeps the same Worker name, class name, binding, and image: change the base class to DurableObject, replace containerFetch() with getTcpPort(port).fetch() plus your own routing, and replace sleepAfter with setInactivityTimeout(). Sandboxes and per-instance images require the direct API and the durable_object policy, not the Container class.
scheduling_policy: central config vs per-instance control
Section titled “scheduling_policy: central config vs per-instance control”The scheduling policy is chosen when the Container application is created and cannot be changed on an existing application; switching policies means creating a replacement application and cutting traffic over (Scheduling Policies, Migrate to the Durable Object scheduling policy).
| Policy | Where image and size are set | How image updates apply | Typical use |
|---|---|---|---|
default (omit field) |
Wrangler image, instance_type, etc. |
Application-wide Rollouts | Same image and size for every instance |
durable_object (beta) |
ctx.container.start({ image, instance, … }) |
Your code decides when to stop and restart with a new digest | Sandboxes, agents, per-session images |
The durable_object policy is in beta. It lets each Durable Object supply its Container image or snapshot and instance size to ctx.container.start(). Set scheduling_policy to “durable_object” explicitly. The linked Durable Object must use SQLite storage.
— Cloudflare, Wrangler configuration — durable object scheduling policy
For durable_object, Wrangler accepts only a narrow containers shape: named images (Dockerfile or digest-pinned Cloudflare registry reference), class_name or name linkage to the Durable Object, plus limited fields such as observability, ssh, and authorized_keys. Fields like max_instances, placement constraints, and default-policy rollouts do not apply (Wrangler configuration, Scheduling Policies).
Named images (up to 100) appear on ctx.container.images after deploy. Updating the map does not restart running containers; only the next start() picks up the new digest (Image Management).
Container lifecycle: cold start, readiness, sleep, and monitor()
Section titled “Container lifecycle: cold start, readiness, sleep, and monitor()”Flow in short: stopped → start() → running but not necessarily ready → ready for traffic → stop via inactivity, signal, destroy(), rollout, or process exit (Lifecycle of a Container).
A cold start is the first boot of an instance from a fully stopped state—often on the order of one to three seconds, depending on image size and entrypoint work (Frequently Asked Questions, Lifecycle of a Container).
Placement is optimized for startup speed, not colocation with the Durable Object:
Durable Objects and their associated Container instances are not guaranteed to run in the same location.
— Cloudflare, Lifecycle of a Container
Once an instance is running, later requests for the same Durable Object id keep using that location until the instance stops; after sleep, the next start may land elsewhere (Frequently Asked Questions).
With the Durable Object Container API, start() returns before readiness; use application health checks (many examples poll getTcpPort(8080).fetch("http://container/health")) or monitor() to observe exit and errors (Monitor container lifecycle, Environment variables and secrets). A pending monitor() can keep the Durable Object from evicting for up to 15 minutes (Durable Object Container API).
The Container class adds sleepAfter (default "10m") and hooks such as onStart and onStop (Container class). Under durable_object, implement the same behavior with setInactivityTimeout() and monitor() instead.
Routing requests: session id, named instances, and getRandom
Section titled “Routing requests: session id, named instances, and getRandom”Every request should enter the Worker’s fetch handler, then reach a specific Durable Object (and its container).
Stateful or per-entity workloads: use a stable name or id—for example getContainer(env.MY_CONTAINER, sessionId) or env.MY_CONTAINER.getByName(pathname) (Get started, Scaling and Routing).
Stateless pool (default policy + Container class): getRandom(env.MY_CONTAINER, N) picks one of N fixed instances per request. Built-in autoscaling for stateless apps is not available yet; you choose N explicitly (Frequently Asked Questions, Scaling and Routing).
For durable_object HTTP routing, patterns in Stateless instances and Static frontend, container backend use getByName(\instance-${index}`)with readiness gating beforegetTcpPort(8080).fetch(). WebSocket upgrades should return the port response directly ([WebSocket to container](https://developers.cloudflare.com/containers/examples/websocket/)); the Container class notes that only fetch()` supports WebSocket proxying (Container class — fetch).
Images, wrangler deploy, and rollout modes
Section titled “Images, wrangler deploy, and rollout modes”Docker locally: If Wrangler points at a Dockerfile path, Docker (or a compatible engine) must be running for wrangler deploy and for local wrangler dev. Registry references do not require Docker at deploy time (Deploy Containers, Get started).
Deploy order: The Worker goes live first; image build/push and container rollout are not transactional—failures can occur after the new Worker is already active (Deploy Containers).
Workers Builds: For Workers that use Containers, production deploys should use wrangler deploy on the production branch; preview flows that only run wrangler versions upload do not update container images or instances (Workers CI/CD Builds).
Default-policy rollouts replace instances on configuration change: SIGTERM, up to 15 minutes to exit, then SIGKILL if needed, then start on the new image (Rollouts).
| Mode | Flag | Effect |
|---|---|---|
| Gradual (default) | (omit) | rollout_step_percentage (often [10, 100] when max_instances ≥ 2) |
| Immediate | --containers-rollout=immediate |
Target 100% in one step; shortens mixed Worker/image window |
| Worker only | --containers-rollout=none |
Worker updates; running container image unchanged |
Align breaking Worker/image pairs with immediate rollout when needed (Rollouts, Deploy a Sandbox application).
Image sources: Default policy supports Dockerfile build, Cloudflare registry, Docker Hub, Amazon ECR, and Google Artifact Registry in Wrangler config. durable_object named images must be Dockerfile-built or digest-pinned on registry.cloudflare.com; pull external registries by pushing to Cloudflare’s registry first (Image Management).
Runtime env vars for the container process can be passed via env on ctx.container.start(); secrets should be read in the Durable Object before start. Values set at start stay fixed until the next start (Environment variables and secrets). The runtime also injects identifiers such as CLOUDFLARE_DURABLE_OBJECT_ID (Environment Variables).
Local development limits and common mistakes
Section titled “Local development limits and common mistakes”Local development runs containers in Docker on your machine. durable_object policy needs Wrangler 4.136.0+ locally; the managed cloudflare/debian-trixie image needs 4.141.0+.
Common pitfalls:
- Assuming
start()orrunningmeans the port is listening—always probe readiness (Durable Object Container API — running). - Using the Container class with
scheduling_policy: "durable_object"(Container class). - Expecting platform rollouts to update
durable_objectsandboxes (Rollouts, Sandbox lifetime). - First deploy: Worker URL may work while container routes fail until provisioning finishes (Get started, Deploy Containers).
- Code that relies on hostname length or non-root Linux capabilities may pass locally but fail when deployed (Local development — differences after deploy).
- Storing secrets in Docker
build_vars; build args are not runtime env (Environment variables and secrets).
Cron tasks using monitor() should stay within Cron Trigger limits and treat overlapping runs as non-queueing (Cron container).
Further patterns live under Examples (stateless pools, env vars, WebSockets, cron). Containers APIs links the full Durable Object Container API reference and Connect to Workers and Bindings.
How to check this on your site
Section titled “How to check this on your site”-
Confirm Docker is running if your Wrangler config uses a Dockerfile path (
docker infoshould succeed), as described in Deploy Containers. -
Deploy with
npx wrangler deploy(or your package manager’s equivalent) and wait for the command to finish; on first deploy, allow several minutes before expecting container routes to work (Get started). -
In the Cloudflare dashboard, confirm the active Worker deployment, then send a request that must hit the container and verify the response (Deploy Containers — Check your deploy).
-
Run
npx wrangler containers listandnpx wrangler containers images list, or open Containers in the dashboard (sidebar: Workers & Pages → Containers), and check instance status and logs (Get started — Check deployment status). -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about so it can be corrected or explained.