How images and running Container instances update depends on the scheduling policy for the application.
With the durable_object policy, Wrangler prepares the named images for the application. Durable Object code can access their immutable references and selects an image and instance size when it calls ctx.container.start(). When an image reference changes, the Durable Object code decides whether to stop the running Container and start it with the new image or let it continue with the previous image. This application-controlled restart is how you roll out image changes with the durable_object policy. These instances do not participate in application-wide image rollouts.
With the default policy, Wrangler uploads or resolves the application image. Cloudflare distributes that image across its network and prepares capacity for new instances. Changes to the image or instance type use a rollout.
Worker code goes live on deploy before any application-wide Container rollout finishes. Refer to Deploy Containers.
flowchart LR
accTitle: Container instance lifecycle
accDescr: A Worker accesses a container through its Durable Object. The container moves from stopped to starting, running but not ready, ready for traffic, stopping, and stopped.
Worker["Worker"] -->|Durable Object binding| DurableObject["Durable Object"]
DurableObject -->|<code>ctx.container</code>| Container
subgraph Container["Container instance"]
direction TD
Stopped["Stopped"]
Starting["Starting<br/><code>start()</code> called"]
Running["Running<br/>Not ready"]
Ready["Ready<br/>Accepting traffic"]
Stopping["Stopping<br/>Stop requested"]
StoppedAgain["Stopped"]
Stopped --> Starting --> Running --> Ready --> Stopping --> StoppedAgain
end
A Container can only be accessed through its Durable Object. A Worker sends a request to the Durable Object, which accesses the Container through ctx.container.
An inactivity timeout, signal(), destroy(), a rollout, or a process exit can stop the instance. If startup fails or the process exits early, the instance returns to the stopped state.
The ctx.container.running property becomes true before the process is ready to accept traffic. Check port readiness before you send the first request.
Manage this lifecycle through the Durable Object Container API. It lets the Durable Object coordinate container compute with persistent state and alarms.
Recall that Containers are backed by Durable Objects and Workers. Requests are first routed through a Worker, which is generally handled by a datacenter in a location with the best latency between itself and the requesting user. A different datacenter may be selected to optimize overall latency, if Smart Placement is on, or if the nearest location is under heavy load.
HTTP requests reach a Container through the Worker's fetch() handler.
For raw TCP traffic, a Spectrum application with a Worker origin invokes the Worker's connect() handler. The Worker can forward the socket through the Container's Durable Object to a process listening on a container port. This path supports protocols such as bidirectional gRPC. Refer to Bidirectional gRPC with Workers and Containers for a complete example.
From the Worker, a request passes through a Durable Object instance. The Durable Object class extends DurableObject and controls the container through ctx.container.
Each Durable Object instance is a globally routable isolate that can execute code and store state. This allows
developers to address and route to specific container instances, run code when a container exits, and store persistent state associated with each instance.
When a Durable Object requests a new Container instance, Cloudflare selects eligible capacity with the required image available. The durable_object policy uses the image and instance options supplied to ctx.container.start(). The default policy uses the application image and instance type from Wrangler configuration.
Additional Container instances may start in different eligible locations, depending on available capacity and image availability. You are only charged for actively running instances, not for prepared images that are not running.
A cold start is when a container instance is started from a completely stopped state.
If you call env.MY_CONTAINER.get(id) with a completely novel ID and launch
this instance for the first time, it will result in a cold start.
This will start the container image from its entrypoint for the first time. Depending
on what this entrypoint does, it will take a variable amount of time to start.
Container cold starts can often be in the 1-3 second range, but this is dependent on image size and code execution time, among other factors.
When a request starts a new Container instance, Cloudflare selects a suitable location based on request routing, available capacity, and image availability. Subsequent requests to that instance reach the same location for as long as the instance runs, regardless of where the requests originate.
After the Container stops, its next start may use a different eligible location. Refer to Placement for details, including how Durable Object jurisdiction affects placement.
Each container instance runs in a microVM with its own kernel and network. No other workload on the Cloudflare network shares that kernel. Your image runs as a Linux container inside the VM.
Containers should be built for the linux/amd64 architecture, and should stay within
size limits.
Logging, metrics collection, and networking are automatically set up on each container, as configured by the developer.
Call setInactivityTimeout() to let the runtime stop the container after the Durable Object becomes inactive. The Durable Object becomes inactive when it stops receiving requests. The timeout can keep the container available while the Durable Object sleeps. You can also stop a container with signal() or destroy().
During a planned stop, such as host maintenance, Cloudflare can send SIGTERM to your container before stopping it. The time available for cleanup depends on what triggered the stop. Handle SIGTERM in your image if you need to clean up before exit.
For image updates on the default scheduling policy, refer to How a container instance is replaced.
The Durable Object runs its own code when the container changes state:
- Startup work runs after
start()and a successful port readiness check. - Exit work runs when the
monitor()promise settles. The promise resolves when the main process exits with code0. It rejects with an error that has anexitCodeproperty when the process exits with another code. - Scheduled work runs from an alarm, whether or not the container is running.
Refer to the status hooks example for a full implementation.
All disk is ephemeral by default. When a Container instance goes to sleep, the next time it starts, it uses a fresh disk from the container image.
If you need point-in-time filesystem state, Container applications that use the durable_object scheduling policy can create and restore a snapshot.
Snapshots are immutable, so later file changes require a new snapshot. For more
information, refer to Snapshots.
You can also use FUSE to persist disk to R2 or other object storage backends. Though you should not expect native SSD-like performance while using FUSE.
- A developer deploys a Worker with a Container application using the
durable_objectscheduling policy and a named image. - A request is made from a client in Bariloche, Argentina. It reaches the Worker in a nearby Cloudflare location in Neuquen, Argentina.
- This Worker request calls
env.MY_CONTAINER.getByName("session-1337"). This brings up a Durable Object, which starts the Container usingthis.ctx.container.start({ image: this.ctx.container.images.base, enableInternet: false }). - Cloudflare selects a suitable location based on request routing, available capacity, and image availability. In this example, it starts the Container in Buenos Aires, Argentina.
- A different user needs to route to the same container. This user's request reaches the Worker running in Cloudflare's location in San Diego, US.
- The Worker again calls
env.MY_CONTAINER.getByName("session-1337"). - If the initial container instance is still running, the request is routed to the original location in Buenos Aires. If the initial Container has stopped, the Durable Object can start a new instance. Cloudflare selects an eligible location for that instance, which may differ from the original location.