- Notifications
You must be signed in to change notification settings - Fork24
Enhance your Workers with serverless containers
cloudflare/containers
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A class for interacting with Containers on Cloudflare Workers.
- HTTP request proxying and WebSocket forwarding
- Simple container lifecycle management (starting and stopping containers)
- Event hooks for container lifecycle events (onStart, onStop, onError)
- Configurable sleep timeout that renews on requests
- Load balancing utilities
npm install @cloudflare/containers
import{Container,getContainer,getRandom}from'@cloudflare/containers';exportclassMyContainerextendsContainer{// Configure default port for the containerdefaultPort=8080;// After 1 minute of no new activity, shutdown the containersleepAfter='1m';}exportdefault{asyncfetch(request,env){constpathname=newURL(request.url).pathname;// If you want to route requests to a specific container,// pass a unique container identifier to .get()if(pathname.startsWith('/specific/')){// In this case, each unique pathname will spawn a new containerconstcontainer=env.MY_CONTAINER.getByName(pathname);returnawaitcontainer.fetch(request);}// Note: this is a temporary method until built-in autoscaling and load balancing are added.// If you want to route to one of many containers (in this case 5), use the getRandom helper.// This load balances incoming requests across these container instances.letcontainer=awaitgetRandom(env.MY_CONTAINER,5);returnawaitcontainer.fetch(request);},};
TheContainer class that extends a container-enbled Durable Object to provide additional container-specific functionality.
defaultPort?Optional default port to use when communicating with the container. If this is not set, or you want to target a specific port on your container, you can specify the port with
fetch(switchPort(req, 8080))orcontainerFetch(req, 8080).requiredPorts?Array of ports that should be checked for availability during container startup. Used by
startAndWaitForPortswhen no specific ports are provided.sleepAfterHow long to keep the container alive without activity (format: number for seconds, or string like "5m", "30s", "1h").
Defaults to "10m", meaning that after the Container class Durable Object receives no requests for 10 minutes, it will shut down the container.
The following properties are used to set defaults when starting the container, but can be overriden on a per-instance basis by passing in values tostartAndWaitForPorts() orstart().
env?: Record<string, string>Environment variables to pass to the container when starting up.
entrypoint?: string[]Specify an entrypoint to override image default.
enableInternet: booleanWhether to enable internet access for the container.
Defaults to
true.pingEndpoint: stringSpecify an endpoint the container class will hit to check if the underlying instance started.This does not need to be set by the majority of people, only use it if you would like the container supervisorto hit another endpoint in your container when it starts it.Observe that
pingEndpointcan include both the hostname and the path. You cansetcontainer/health, meaning"container"will be the value passed along theHostheader, and"/health"the path.Defaults to
ping.
These lifecycle methods are automatically called when the container state transitions. Override these methods to use these hooks.
Seethis example.
onStart()Called when container starts successfully.
- called when states transition from
stopped->running,running->healthy
- called when states transition from
onStop()Called when container shuts down.
onError(error)Called when container encounters an error, and by default logs and throws the error.
onActivityExpired()Called when the activity is expired. The container will run continue to run for some time after the last activity - this length of time is configured by
sleepAfter.By default, this stops the container with aSIGTERM, but you can override this behaviour, as with other lifecycle hooks. However, if you don't stop the container here, the activity tracker will be renewed, and this lifecycle hook will be called again when the timer re-expires.
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>Forwards HTTP requests to the container.
If you want to target a specific port on the container, rather than the default port, you should use
switchPortlike so:constcontainer=env.MY_CONTAINER.getByName('id');awaitcontainer.fetch(switchPort(request,8080));
Make sure you provide a port with switchPort or specify a port with the
defaultPortproperty.You must use
fetchrather thancontainerFetchif you want to forward websockets.Note that when you call any of the fetch functions, the activity will be automatically renewed (sleepAfter time starts after last activity), and the container will be started if not already running.
containerFetch(...)Note:
containerFetchdoes not work with websockets.Sends an HTTP request to the container. Supports both standard fetch API signatures:
containerFetch(request, port?): Traditional signature with Request objectcontainerFetch(url, init?, port?): Standard fetch-like signature with URL string/object and RequestInit options
startAndWaitForPorts(args: StartAndWaitForPortsOptions): Promise<void>Starts the container and then waits for specified ports to be ready. Prioritises
portspassed in to the function, thenrequiredPortsif set, thendefaultPort.interfaceStartAndWaitForPortsOptions{startOptions?:{/** Environment variables to pass to the container */envVars?:Record<string,string>;/** Custom entrypoint to override container default */entrypoint?:string[];/** Whether to enable internet access for the container */enableInternet?:boolean;};/** Ports to check */ports?:number|number[];cancellationOptions?:{/** Abort signal to cancel start and port checking */abort?:AbortSignal;/** Max time to wait for container to start, in milliseconds */instanceGetTimeoutMS?:number;/** Max time to wait for ports to be ready, in milliseconds */portReadyTimeoutMS?:number;/** Polling interval for checking container has started or ports are ready, in milliseconds */waitInterval?:number;};}
start(startOptions?: ContainerStartConfigOptions, waitOptions?: WaitOptions)Starts the container, without waiting for any ports to be ready.
You might want to use this instead of
startAndWaitForPortsif you want to:- Start a container without blocking until a port is available
- Initialize a container that doesn't expose ports
- Perform custom port availability checks separately
Options:
interfaceContainerStartConfigOptions{/** Environment variables to pass to the container */envVars?:Record<string,string>;/** Custom entrypoint to override container default */entrypoint?:string[];/** Whether to enable internet access for the container */enableInternet?:boolean;}interfaceWaitOptions{/** The port number to check for readiness */portToCheck:number;/** Optional AbortSignal, use this to abort waiting for ports */signal?:AbortSignal;/** Number of attempts to wait for port to be ready */retries?:number;/** Time to wait in between polling port for readiness, in milliseconds */waitInterval?:number;}
stop(signal = SIGTERM): Promise<void>Sends the specified signal to the container. Triggers
onStop.destroy(): Promise<void>Forcefully destroys the container (sends
SIGKILL). TriggersonStop.getState(): Promise<State>Get the current container state.
typeState={lastChange:number;}&(|{// 'running' means that the container is trying to start and is transitioning to a healthy status.// onStop might be triggered if there is an exit code, and it will transition to 'stopped'.status:'running'|'stopping'|'stopped'|'healthy';}|{status:'stopped_with_code';exitCode?:number;});
renewActivityTimeout()Manually renews the container activity timeout (extends container lifetime).
schedule<T = string>(when: Date | number, callback: string, payload?: T): Promise<Schedule<T>>Options:
when: When to execute the task (Date object or number of seconds delay)callback: Name of the function to call as a stringpayload: Data to pass to the callback
Instead of using the default alarm handler, use
schedule()instead. The default alarm handler is in charge of renewing the container activity and keeping the durable object alive. You can overridealarm(), but because its functionality is currently vital to managing the container lifecycle, we recommend callingscheduleto schedule tasks instead.
getRandom(binding, instances?: number)Get a random container instances across N instances. This is useful for load balancing.Returns a stub for the container.Seeexample.
getContainer(binding, name?: string)Helper to get a particular container instance stub.e.g.
const container = getContainer(env.CONTAINER, "unique-id")If no name is provided, "cf-singleton-container" is used.
import{Container}from'@cloudflare/containers';exportclassMyContainerextendsContainer{// Configure default port for the containerdefaultPort=8080;// Set how long the container should stay active without requests// Supported formats: "10m" (minutes), "30s" (seconds), "1h" (hours), or a number (seconds)sleepAfter='10m';// Lifecycle method called when container startsoverrideonStart():void{console.log('Container started!');}// Lifecycle method called when container shuts downoverrideonStop():void{console.log('Container stopped!');// you can also call startAndWaitForPorts() again// this.startAndWaitForPorts();}// Lifecycle method called on errorsoverrideonError(error:unknown):any{console.error('Container error:',error);throwerror;}// Lifecycle method when the container class considers the activity to be expiredoverrideonActivityExpired(){console.log('Container activity expired');awaitthis.destroy();}// Custom method that will extend the container's lifetimeasyncperformBackgroundTask():Promise<void>{// Do some work...// Renew the container's activity timeoutawaitthis.renewActivityTimeout();console.log('Container activity timeout extended');}// Additional methods can be implemented as needed}
The Container class automatically supports proxying WebSocket connections to your container. WebSocket connections are bi-directionally proxied, with messages forwarded in both directions. The Container also automatically renews the activity timeout when WebSocket messages are sent or received.
// Connect to a WebSocket on port 9000constresponse=awaitcontainer.fetch(switchPort(request,9000));
Note websockets are not supported withcontainerFetch.
You can configure defaults for how the container starts by setting the instance properties for environment variables, entrypoint, and network access:
import{Container}from'@cloudflare/containers';exportclassConfiguredContainerextendsContainer{// Default port for the containerdefaultPort=9000;// Set the timeout for sleeping the container after inactivitysleepAfter='2h';// Environment variables to pass to the containerenvVars={NODE_ENV:'production',LOG_LEVEL:'info',APP_PORT:'9000',};// Custom entrypoint to run in the containerentrypoint=['node','server.js','--config','production.json'];// Enable internet access for the containerenableInternet=true;// These configuration properties will be used automatically// when the container starts}
You can also set these on a per-instance basis withstart orstartAndWaitForPorts
You can create a container that doesn't use a default port and instead routes traffic to different ports based on request path or other factors:
import{Container}from'@cloudflare/containers';exportclassMultiPortContainerextendsContainer{// No defaultPort defined - we'll handle port specification manuallyconstructor(ctx:any,env:any){super(ctx,env);}/** * Process an incoming request and route to different ports based on path */asyncfetch(request:Request):Promise<Response>{consturl=newURL(request.url);try{if(url.pathname.startsWith('/api')){// API server runs on port 3000returnawaitthis.containerFetch(request,3000);}elseif(url.pathname.startsWith('/admin')){// Admin interface runs on port 8080returnawaitthis.containerFetch(request,8080);}else{// Public website runs on port 80returnawaitthis.containerFetch(request,80);}}catch(error){returnnewResponse(`Error:${errorinstanceofError ?error.message :String(error)}`,{status:500,});}}}
You can use the containerFetch method with standard fetch API syntax:
import{Container}from'@cloudflare/containers';exportclassFetchStyleContainerextendsContainer{defaultPort=8080;asynccustomHandler():Promise<Response>{try{// Using the new fetch-style syntaxconstresponse=awaitthis.containerFetch('/api/data',{method:'POST',headers:{'Content-Type':'application/json',},body:JSON.stringify({query:'example'}),});// You can also specify a port with this syntaxconstadminResponse=awaitthis.containerFetch('https://example.com/admin',{method:'GET'},3000// port);returnresponse;}catch(error){returnnewResponse(`Error:${errorinstanceofError ?error.message :String(error)}`,{status:500,});}}}
The Container class includes an automatic idle timeout feature that will shut down the container after a period of inactivity. This helps save resources when containers are not in use.
import{Container}from'@cloudflare/containers';exportclassTimeoutContainerextendsContainer{// Configure default port for the containerdefaultPort=8080;// Set timeout to 30 minutes of inactivitysleepAfter='30m';// Supports "30s", "5m", "1h" formats, or a number in seconds// Custom method that will extend the container's lifetimeasyncperformBackgroundTask(data:any):Promise<void>{console.log('Performing background task...');// Manually renew the activity timeout, even though// you have not made a request to the containerawaitthis.renewActivityTimeout();console.log('Container activity timeout renewed');}// Activity timeout is automatically renewed on fetch requestsasyncfetch(request:Request):Promise<Response>{consturl=newURL(request.url);// Example endpoint to trigger background taskif(url.pathname==='/task'){awaitthis.performBackgroundTask();returnnewResponse(JSON.stringify({success:true,message:'Background task executed',nextStop:`Container will shut down after${this.sleepAfter} of inactivity`,}),{headers:{'Content-Type':'application/json'}});}// For all other requests, forward to the container// This will automatically renew the activity timeoutreturnthis.containerFetch(request);}}
This package includes agetRandom helper which routes requests to one of N instances.In the future, this will be automatically handled with resource-aware load balancingwhen autoscaling is set to true, but it is not yet implemented.
import{Container,getContainer,getRandom}from'@cloudflare/containers';exportclassMyContainerextendsContainer{defaultPort=8080;}exportdefault{asyncfetch(request:Request,env:any){consturl=newURL(request.url);// Example: Load balance across 5 container instancesif(url.pathname==='/api'){constcontainerInstance=awaitgetRandom(env.MY_CONTAINER,5);returncontainerInstance.fetch(request);}// Example: Direct request to a specific containerif(url.pathname.startsWith('/specific/')){constid=url.pathname.split('/')[2]||'default';constcontainerInstance=getContainer(env.MY_CONTAINER,id);returncontainerInstance.fetch(request);}returnnewResponse('Not found',{status:404});},};
About
Enhance your Workers with serverless containers
Topics
Resources
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.