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.
npm i @cloudflare/containers
yarn add @cloudflare/containers
pnpm add @cloudflare/containers
Then, you can define a class that extends Container
, and use it in your Worker:
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.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark