Cloudflare Containers Quickstart
You can now use Cloudflare Containers (Beta) to run processes which require a full Linux environment including a filesystem.

How it works.
-
wrangler devbuilds container images using a Dockerfile. The docker build process runs commands from the Dockerfile to install packages and copy files. The resulting filesystem changes are captured as cacheable layers in the container image. -
The image can run locally during development and gets pushed to the Cloudflare registry on deploy.
-
The
containerskey inwrangler.jsoncassociates the image with a Durable Object (DO) class. The implementation of this class is exported from your worker code. It extends aContainerclass which itself extendsDurableObject. -
Container Durable Object instances map 1:1 to running containers. A worker’s fetch handler can
get()a container instance by name/id, and route HTTP requests to it viacontainer.fetch(). Those requests are then forwarded by the Durable Object to the exposed port on the running container instance. Containers can spin up lazily, on the first request to the container.
Example
The example in github.com/jldec/container-demo-terminal provides a minimal implementation of the flow described above.

-
The
Dockerfileadds a web-based terminal called tty2web to the image. -
wrangler.jsoncassociates the image with theContainerClass. -
index.tsis the entry point for the worker. It exportsContainerClassand uses theCONTAINER_DObinding to connect to the container. Adding a query parameter like?id=xxwill spin up multiple instances with different names.
The worker is deployed at https://container-demo-terminal.jldec.workers.dev. Visitors interact with a bash shell in the container over a websocket. This URL is protected with Cloudflare Access.

Cloudflare’s dashboard provides nice observability.

Sandboxes FTW
What if you could provide your users with their own on-demand containers, where they can safely run AI-generated code? 🤖
The Cloudflare Sandbox SDK is built on Cloudflare Containers. It includes a general purpose container with git, Python, nodejs and an API to run commands and streaming responses.
Check out the documentation for more details.
Conclusion
I hope this quick intro helps to demystify Cloudflare Containers and Durable Objects a little bit. Please reach out on Twitter with questions or feedback.