Skip to content
Merged
54 changes: 54 additions & 0 deletions .changeset/container-durable-object-scheduling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
'devflare': minor
---

Containers can use Cloudflare's `durable_object` scheduling policy, where the Durable Object picks
the image and instance size each time it calls `ctx.container.start()`. Cloudflare's
scheduling-policy docs mark the `default` policy deprecated, and its 2026-09-30 announcement says
the `Container` class from `@cloudflare/containers` gets no updates after 2026-12-31, so this is the
shape new containers should take.

```ts
containers: [
{
className: 'Builder',
schedulingPolicy: 'durable_object',
images: { base: { dockerfile: './container/Dockerfile' } }
}
]
```

**It compiles to exactly what Wrangler takes for that policy:** `class_name`, `scheduling_policy`,
`name` and `images` (each a `{ dockerfile, build_context?, build_vars? }` or a digest-pinned
`{ image }`). The schema refuses `image`, `instanceType`, `maxInstances` and the rollout fields on
such an entry, as Wrangler does. Existing entries parse and compile unchanged; `schedulingPolicy:
'default'` may now be spelled out.

**`devflare dev` and `createTestContext()` run it locally, as `wrangler dev` does.** Devflare builds
each Dockerfile image (or reuses or pulls a registry image) with Docker or Podman for `linux/amd64`,
pulls Miniflare's egress interceptor image, and attaches the container to its Durable Object in
Miniflare with SQLite storage. `ctx.container.images`, `start({ enableInternet: false })` and the
`interceptOutboundHttp()`, `interceptOutboundHttps()` and `interceptAllOutboundHttp()` rules then
run against a real local container. In `createTestContext()`, the `WorkerEntrypoint` classes a
container-backed Durable Object's module exports are exported from the test gateway too, so an
intercept can target `ctx.exports.<Name>`.

Local containers need Linux or macOS: workerd cannot reach a Windows named pipe, so on Windows
devflare refuses and points at WSL, as Wrangler does. `DEVFLARE_ENABLE_CONTAINERS=0` starts without
them, leaving `ctx.container` undefined. `devflare workspace dev` does not run them yet and refuses
an app that declares one, and `containers.start()` refuses a `durable_object` entry, which only its
Durable Object can start.

**A worker that is only referenced runs its containers too.** When the config reaches a worker
through a `ref()` service binding (in `createTestContext()` and in `devflare dev`), or binds a Durable
Object class of another worker (in `createTestContext()`), that worker's `durable_object` containers
are built from its own config and attached where devflare hosts its Durable Objects, with the
intercept targets its modules export. So a test
can drive a container through the service binding that production uses. A config that references
such a worker now needs an engine in those runs (or `DEVFLARE_ENABLE_CONTAINERS=0`), even when it
declares no container itself. `devflare workspace dev` refuses an app that reaches one through a service
binding, and the Vite plugin leaves a referenced worker's containers out, so `ctx.container` is
undefined there.

**For TypeScript consumers:** a `containers` entry's type is now a union of the two policies'
shapes, so code that reads `config.containers[i].image` must first check `schedulingPolicy`.
74 changes: 73 additions & 1 deletion .github/workflows/workspace-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,76 @@ jobs:

- name: Run cached devflare validation lane
shell: bash
run: bun run devflare:ci
run: bun run devflare:ci

# Starts a real durable_object scheduling-policy container through
# createTestContext() and devflare dev: devflare builds the image, workerd drives it through
# ctx.container, and the test checks enableInternet: false and the outbound
# intercepts. The runner's Docker engine is what the validate lane lacks a
# flag for, so it runs here alone, and a skip fails the job.
durable-object-containers:
runs-on: ubuntu-latest
timeout-minutes: 30
env:
DEVFLARE_CONTAINER_TESTS: "1"
DEVFLARE_CONTAINER_TESTS_REQUIRED: "1"
DEVFLARE_CONTAINER_ENGINE: docker

steps:
- name: Checkout
uses: actions/checkout@v5

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.2

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "22"

- name: Restore Bun install cache
uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: ${{ runner.os }}-bun-1.4.2-${{ hashFiles('bun.lock') }}
restore-keys: |
${{ runner.os }}-bun-1.4.2-

- name: Install workspace dependencies
shell: bash
run: bun install --frozen-lockfile

- name: Docker engine
shell: bash
run: docker info --format '{{.ServerVersion}} {{.OperatingSystem}}'

- name: Run the durable_object container test through createTestContext
shell: bash
working-directory: packages/devflare
run: bun test tests/integration/test-context/durable-object-container.test.ts --max-concurrency=1

# devflare dev hosts the object in a worker of its own, so it is graded separately.
- name: Run the durable_object container test through devflare dev
shell: bash
working-directory: packages/devflare
run: bun test tests/integration/dev-server/durable-object-container-dev.test.ts --max-concurrency=1

# A worker the test only references runs its containers too: through a service
# binding, through a cross-worker Durable Object binding, and under devflare dev.
# One process each, since a test context is process-wide.
- name: Run the referenced worker's container through a service binding
shell: bash
working-directory: packages/devflare
run: bun test tests/integration/test-context/referenced-durable-object-container.test.ts --max-concurrency=1

- name: Run the referenced worker's container through a cross-worker Durable Object
shell: bash
working-directory: packages/devflare
run: bun test tests/integration/test-context/referenced-durable-object-container-do-ref.test.ts --max-concurrency=1

- name: Run the referenced worker's container through devflare dev
shell: bash
working-directory: packages/devflare
run: bun test tests/integration/dev-server/referenced-durable-object-container-dev.test.ts --max-concurrency=1
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ test('creates an in-memory artifact repo', async () => {
categoryDescription:
'Cloudflare Containers config plus a Worker route that hands requests to a container-backed Durable Object.',
configKey: 'containers',
authoringShape: 'Array<{ className; image; maxInstances?; instanceType?; imageBuildContext? }>',
authoringShape:
"Array<{ className; image; maxInstances?; instanceType?; imageBuildContext? } | { className; schedulingPolicy: 'durable_object'; images? }>",
localStory:
'Offline-native only when an explicit Docker/Podman engine is available and the image can run without pulling',
'Offline-native only when an explicit Docker/Podman engine is available and the image can run without pulling; durable_object containers run under workerd on Linux or macOS',
sourcePages: [
'packages/devflare/src/config/schema-runtime.ts',
'packages/devflare/src/config/compiler.ts',
Expand All @@ -396,6 +397,10 @@ test('creates an in-memory artifact repo', async () => {
'https://developers.cloudflare.com/containers/get-started/',
'https://developers.cloudflare.com/containers/platform-details/image-management/',
'https://developers.cloudflare.com/containers/container-class/',
'https://developers.cloudflare.com/containers/configuration/scheduling-policy/',
'https://developers.cloudflare.com/containers/api/durable-object-container/',
'https://developers.cloudflare.com/containers/platform-details/outbound-traffic/',
'https://blog.cloudflare.com/faster-agent-sandboxes/',
'https://developers.cloudflare.com/workers/wrangler/configuration/#containers'
],
compileTarget: 'Wrangler `containers`',
Expand Down Expand Up @@ -476,6 +481,88 @@ test('container engine detection is explicit', async () => {
})`
},
overviewSections: [
{
id: 'container-durable-object-policy',
title: 'Let the Durable Object choose the image: the durable_object scheduling policy',
paragraphs: [
"Cloudflare's `durable_object` scheduling policy (public beta) moves the image and instance size out of config and into `ctx.container.start()`. Cloudflare's scheduling-policy page marks the older `default` policy deprecated, and its 2026-09-30 announcement says the `@cloudflare/containers` `Container` class gets no updates after 2026-12-31, so new containers should use this one. The policy is immutable per application: moving an existing application means a new one.",
"Set `schedulingPolicy: 'durable_object'` and list the images the object may start under `images`. Each key becomes a property of `ctx.container.images`. Devflare compiles the entry to Wrangler's `class_name`, `scheduling_policy`, `name` and `images` and nothing else, because Wrangler rejects `image`, `instance_type`, `max_instances` and rollouts for this policy; the schema refuses them first.",
"`devflare dev` and `createTestContext()` run it locally the way `wrangler dev` does: they build each Dockerfile image (or reuse or pull a registry image) with Docker or Podman for `linux/amd64`, pull Miniflare's egress interceptor image, and attach the container to the Durable Object in Miniflare. `ctx.container.start()`, `images`, `enableInternet` and the `interceptOutboundHttp()`, `interceptOutboundHttps()` and `interceptAllOutboundHttp()` rules then run against a real local container."
],
snippets: [
{
title: 'Declare a Durable Object-managed container',
language: 'ts',
code: String.raw`import { defineConfig } from 'devflare/config'

export default defineConfig({
name: 'builder-worker',
compatibilityDate: '2026-09-29',
files: { durableObjects: 'src/do.*.ts' },
bindings: {
durableObjects: { BUILDER: 'Builder' }
},
containers: [
{
className: 'Builder',
schedulingPolicy: 'durable_object',
images: {
base: { dockerfile: './container/Dockerfile' }
}
}
],
migrations: [{ tag: 'v1', new_sqlite_classes: ['Builder'] }]
})`
},
{
title: 'Start it from the Durable Object, with the Internet off and one host allowed',
language: 'ts',
code: String.raw`import { DurableObject, WorkerEntrypoint } from 'cloudflare:workers'

export class AllowRelay extends WorkerEntrypoint {
fetch(request: Request) {
const url = new URL(request.url)
if (url.hostname !== 'relay.example.com') return new Response('Forbidden', { status: 403 })
return fetch(request)
}
}

export class Builder extends DurableObject {
async ensureStarted() {
const container = this.ctx.container!
if (container.running) return
await container.interceptOutboundHttps('relay.example.com', this.ctx.exports.AllowRelay)
container.start({ image: container.images.base, enableInternet: false })
}
}`
},
{
title: 'What it compiles to',
language: 'json',
filename: '.devflare/wrangler.jsonc',
code: String.raw`{
"containers": [
{
"class_name": "Builder",
"scheduling_policy": "durable_object",
"images": { "base": { "dockerfile": "./container/Dockerfile" } }
}
]
}`
}
],
bullets: [
'A named image is either `{ dockerfile, buildContext?, buildVars? }` or `{ image }`, a digest-pinned `registry.cloudflare.com/<account-id>/<repository>@sha256:<digest>`. Between 1 and 100 images, each name 1 to 128 characters.',
'The Durable Object class must use SQLite storage. List it in `new_sqlite_classes`; local runs give container-backed namespaces SQLite storage, as Wrangler does.',
"Outbound intercepts target a `WorkerEntrypoint` through `ctx.exports`, which needs a compatibility date of 2025-11-17 or later. In `createTestContext()`, the `WorkerEntrypoint` classes exported from the Durable Object's own module are exported from the test gateway, so `ctx.exports.<Name>` resolves there too. A service binding also works as a target.",
'HTTPS interception needs the container to trust `/etc/cloudflare/certs/cloudflare-containers-ca.crt`.',
"Local runs need Linux or macOS: workerd cannot reach a Windows named pipe, so on Windows devflare refuses and points at WSL, as Wrangler does. Set `DEVFLARE_ENABLE_CONTAINERS=0` to start without containers; `ctx.container` is then undefined. Devflare uses `WRANGLER_DOCKER_HOST` or `DOCKER_HOST` when set, then the engine's own socket.",
'`devflare workspace dev` does not run durable_object containers yet and refuses an app that declares one; run `devflare dev` in that app. The `containers.start()` helper starts only `default`-policy containers.',
'A worker this one only references runs its containers too. Through a `ref()` service binding to it, in `createTestContext()` and in `devflare dev`, or through a cross-worker Durable Object binding to its class, in `createTestContext()`, devflare builds its images from its own config and attaches them where it hosts that Durable Object. So once a worker references one with a container, those runs need an engine (and on Windows, WSL or `DEVFLARE_ENABLE_CONTAINERS=0`) even when its own config declares none.',
"The Vite plugin (`vite dev` of a SvelteKit app) does not attach a referenced worker's containers, so `ctx.container` is undefined in it there. The `devflare dev` path above is tested for a worker-only project; test a SvelteKit app's call into such a worker with `createTestContext()`. `devflare workspace dev` refuses an app that reaches one through a service binding.",
'Images are rebuilt each time the config loads; a change inside the container needs a restart.'
]
},
{
id: 'container-image-workflow',
title: 'Build and reference the image deliberately',
Expand Down
14 changes: 14 additions & 0 deletions docs/CLOUDFLARE_SUPPORT_MATRIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ In the support matrix, containers are classified ✅ full local: Devflare can
launch local Docker/Podman containers in explicit tests when an engine and cached
images are available.

A container under the **`durable_object` scheduling policy** (`schedulingPolicy:
'durable_object'`) runs differently, the way `wrangler dev` runs it: `devflare dev`
and `createTestContext()` build its named images (pulling base layers as Docker
does), pull Miniflare's egress interceptor image, and attach the container to its
Durable Object in Miniflare, so `ctx.container`, `enableInternet` and the
outbound intercepts work against a real local container. That path is not
offline-first, needs Linux or macOS (devflare refuses Windows, as Wrangler does),
and is turned off with `DEVFLARE_ENABLE_CONTAINERS=0`. A worker reached through a
`ref()` service binding runs its containers the same way in both runtimes, and one
reached through a cross-worker Durable Object binding does in `createTestContext()`.
`devflare workspace dev` does not run them yet and refuses an app that declares one
or reaches one through a service binding; the Vite plugin leaves a referenced
worker's containers out.

## How deploy auto-provisioning works

When you deploy, Devflare materializes the resources your bindings reference. The
Expand Down
Loading
Loading