Skip to content
Cloudflare Docs

Container Package

When writing code that interacts with a container instance, you can either use a Durable Object directly or use the Container class importable from @cloudflare/containers.

We recommend using the Container class for most use cases.

Terminal window
npm i @cloudflare/containers

Then, you can define a class that extends Container, and use it in your Worker:

JavaScript
import { Container } from "@cloudflare/containers";
class MyContainer extends Container {
defaultPort = 8080;
sleepAfter = "5m";
}
export default {
async fetch(request, env) {
// gets default instance and forwards request from outside Worker
return env.MY_CONTAINER.getByName("hello").fetch(request);
},
};

The Container class extends DurableObject so all Durable Object functionality is available. It also provides additional functionality and a nice interface for common container behaviors, such as:

  • sleeping instances after an inactivity timeout
  • making requests to specific ports
  • running status hooks on startup, stop, or error
  • awaiting specific ports before making requests
  • setting environment variables and secrets

See the Containers GitHub repo for more details and the complete API.