Skip to content

Cloudflare Containers SDK for Python Workers

containers-py is a Python SDK for Cloudflare Containers. It starts and stops containers from a Python Worker and sends requests to them.

containers-py ports v0.3.7 of the @cloudflare/containers TypeScript package. It keeps the same defaults, timeouts and state transitions, so the platform docs apply as written. Python names use snake_case; options are keyword arguments.

from containers import Container, get_container
from workers import Request, Response, WorkerEntrypoint


class MyContainer(Container):
    default_port = 8080  # the port your server listens on
    sleep_after = "10m"  # stop after 10 minutes without requests


class Default(WorkerEntrypoint):
    async def fetch(self, request: Request) -> Response:
        session_id = request.headers.get("x-session-id", "default")
        container = get_container(self.env.MY_CONTAINER, session_id)
        return await container.fetch(request)

How it works

Each container instance belongs to one Durable Object. The Container class is that Durable Object. You subclass it and set class attributes such as the port and the idle timeout. The class starts the container on the first request, waits for its port, forwards requests to it and stops it after sleep_after without traffic.

A request takes this path:

  1. A client sends a request to your Worker's Default entrypoint.
  2. The Worker calls get_container(binding, "user-42") to get a stub for the Durable Object named user-42, an instance of your Container subclass.
  3. The Durable Object forwards the request to its container instance, which runs your Docker image.

A Durable Object ID maps to exactly one container instance. The name you pass to get_container decides which instance serves a request:

  • The same name always reaches the same instance, so you can give each user, session or document its own container.
  • get_random(binding, n) spreads stateless requests across n instances.

The Durable Object also stores the container's state, its schedules and its outbound rules in SQLite, so they survive the Durable Object leaving memory.

When to use it

Use a container when the work can't run inside a Python Worker: a binary such as ffmpeg, a package with native code that Pyodide doesn't support, a process that runs for minutes, or a server written in another language.

For work that fits in a Worker, a plain Worker is simpler. It has no image to build and no cold start of a few seconds.

What the SDK covers

  • Starting, stopping and destroying containers, with lifecycle hooks.
  • Routing a request to a named instance, a random instance or another port.
  • Calling your own async methods on the container class from a Worker over RPC.
  • Blocking, allowing or intercepting the container's outbound HTTP and HTTPS traffic with Python handlers.
  • Scheduling a method on the container class to run later.

Install

Add the package to a Python Worker project:

uv add containers-py

It needs Python 3.12 or later and works with either crosswind-workers or workers-runtime-sdk, whichever your project declares. Running a container also needs Docker locally and the Workers Paid plan. To create a project first, follow Getting started.

Status

These pages cover containers-py 0.2.1, which needs Python 3.12 or later. The package is pre-1.0, and its API may change between minor releases. It is an independent community project, not an official Cloudflare SDK.

Next steps