The @cloudflare/containers package is deprecated. Its Container class wraps the Durable Object Container API with lifecycle and routing helpers. Replace these helpers with application code that uses this.ctx.container inside a Durable Object.
You can migrate the implementation without replacing the Durable Object. Keep the Worker name, exported class name, binding, container image, and existing migration tags unchanged. Changing the TypeScript base class does not require a new Durable Object migration.
Before changing code, identify which helpers your application uses. Some helpers require application code to preserve their existing behavior.
Container class |
Durable Object Container API |
|---|---|
start(), stop(), and destroy() |
start(), signal(), and destroy() |
defaultPort, fetch(), and containerFetch() |
getTcpPort(port).fetch() and your own request routing |
startAndWaitForPorts() and waitForPort() |
A readiness check with getTcpPort() in application code |
| Concurrent start handling | One shared startup promise in the Durable Object |
envVars and entrypoint |
The env and entrypoint options of start() |
sleepAfter and onActivityExpired() |
setInactivityTimeout() |
onStart(), onStop(), and onError() |
Application code around start() and monitor() |
schedule() |
Durable Object alarms |
enableInternet |
The enableInternet option of start(). The Container class allows Internet access by default. start() does not. Pass enableInternet: true to keep the old behavior. |
outbound, outboundByHost, allowedHosts, deniedHosts, and interceptHttps |
interceptOutboundHttp(), interceptAllOutboundHttp(), and interceptOutboundHttps() with a WorkerEntrypoint. Refer to Handle outbound traffic. |
getContainer(binding, name) |
binding.getByName(name). Without a name, getContainer(binding) used the name cf-singleton-container. |
getRandom(binding, count) |
binding.getByName(`instance-${Math.floor(Math.random() * count)}`). Keep the instance- prefix to reach the same Durable Objects. Refer to Scaling and routing. |
- In your Worker, change the class to extend
DurableObjectfromcloudflare:workers. Keep the exported class name to retain its existing container definition and Durable Object binding. Do not add a new Durable Object migration solely because you changed the base class. Refer to Wrangler configuration. - Replace
start()calls withctx.container.start(). PassenableInternet: trueif the container needs Internet access. Replacestop()calls withsignal()ordestroy(), as appropriate. Do not assumestart()waits for a port to become ready. - Replace
defaultPort,containerFetch(), and automaticfetch()routing withgetTcpPort(port).fetch()and your own request routing. Check port readiness before forwarding requests. For a complete example, refer to Stateless instances. - Replace
sleepAfterwithsetInactivityTimeout(). Replace lifecycle hooks andschedule()with application code,monitor(), and Durable Object alarms where appropriate. For an example, refer to Status hooks. - Replace outbound properties and handlers with intercepts that you register each time the container starts. Refer to Handle outbound traffic.
- Replace
getContainer()andgetRandom()withgetByName()on the Durable Object namespace. Use the same names, so that requests reach the existing Durable Objects and their storage. - Test startup, concurrent requests, readiness, idle shutdown, alarm delivery, storage continuity, and recovery after a container restart. A container can be temporarily unavailable after
signal()ordestroy(). Retry allocation before checking port readiness. Then remove theContainerProxyexport and any other imports of@cloudflare/containers, and remove the package from your dependencies.
Keep the same image in the containers section of your Wrangler configuration unless you intend to change the container application itself. The direct API still runs the image associated with its Durable Object class.
For process handling and output, refer to Execute commands and the exec() reference.