Skip to content
TypeScript
26.5 KB
Raw Blame
---
title: Containers
description: Cloudflare Containers run long-lived processes beside a Durable Object — declare a typed container class, implement its runtime in a separate file, and alchemy builds the image, pushes it, and wires the DO pairing.
---

A Cloudflare Container is a long-lived process running next to a
[Durable Object](/cloudflare/compute/durable-objects): the DO owns the
container's lifecycle, and callers reach the container through it.
In alchemy a container is a class with a typed RPC surface — the
same tagged-shape ceremony as a Durable Object — plus a runtime
implementation that alchemy bundles into a Docker image and pushes
to Cloudflare's registry for you.

Reach for a container when a [Worker](/cloudflare/compute/workers) isn't
enough: you need a real OS process — a sandboxed shell, a binary you
can't compile to wasm, an HTTP server listening on a port, a
runtime Workers doesn't offer. If you only need per-entity state
and coordination, a plain Durable Object is lighter; if you only
need request/response compute, a Worker is.

## Declare the container class

The class declares the container's name and its public RPC surface;
configuration lives on `.make()`, not here:

```typescript
// src/Sandbox.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import type * as Effect from "effect/Effect";

export class Sandbox extends Cloudflare.Container<
  Sandbox,
  { ping: () => Effect.Effect<string> }
>()("Sandbox") {}
```

Anything that binds `Sandbox` sees `ping()` as a typed RPC method
returning `Effect<string>` — no schemas, no serialization
boilerplate, exactly like DO methods.

## Implement the runtime in a separate file

The runtime always lives in its own file — the DO imports the class,
and if the implementation lived alongside it, the DO bundle would
pull in process spawners, Node APIs, and SDKs that the Workers
runtime rejects. `.make()` takes the deploy-time props first and the
implementation second:

```typescript
// src/Sandbox.runtime.ts
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Sandbox } from "./Sandbox.ts";

export default Sandbox.make(
  { main: import.meta.url },
  Effect.gen(function* () {
    return Sandbox.of({
      ping: () => Effect.succeed("pong"),
      fetch: Effect.succeed(
        HttpServerResponse.text("Hello from the container!"),
      ),
    });
  }),
);
```

`main` tells alchemy to bundle this file's Effect program and bake
it into a generated image as the entrypoint. RPC methods and `fetch`
are independent: `fetch` serves HTTP on port `3000` inside the
container by default, while methods like `ping` are invoked directly
through the typed handle.

## Run it from a Durable Object

Yield the class inside a DO's Construction phase. That binds the
container to the DO and hands you its handle, without starting it:

```typescript
// src/Agent.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import { Sandbox } from "./Sandbox.ts";

export default class Agent extends Cloudflare.DurableObject<Agent>()(
  "Agents",
  Effect.gen(function* () {
    const sandbox = yield* Sandbox;

    return Effect.gen(function* () {
      const start = sandbox.start({ enableInternet: true });
      return {
        ping: () => start.pipe(Effect.andThen(sandbox.ping())),
      };
    });
  }),
) {}
```

`start` is idempotent: it starts the container unless it is already
running, and concurrent calls start it once, so call it at the top of
each operation. RPC methods and `getTcpPort` wait until the container's
port accepts connections, but never start it.

:::note
Stopping, sleeping, crash handling and snapshots are covered in
[Container lifecycle](/cloudflare/compute/container-lifecycle).
:::

Importing `Sandbox` (the class) does not pull in the runtime file —
it gets tree-shaken out of the DO's bundle. A Worker then binds
`Agent` and calls `agents.getByName(name).ping()` like any other DO
method.

## Wire the runtime into the Stack

The `.make()` default export is the side-effect that registers the
container's runtime, so it must be reachable from `alchemy.run.ts`:

```typescript
// alchemy.run.ts
import SandboxLive from "./src/Sandbox.runtime.ts";

export default Alchemy.Stack(
  "MyApp",
  { providers: Cloudflare.providers(), state: Cloudflare.state() },
  Effect.gen(function* () {
    const worker = yield* Worker;
    return { url: worker.url };
  }).pipe(Effect.provide(SandboxLive)),
);
```

On deploy, alchemy bundles the entrypoint, builds and pushes the Docker
image as needed, and reconciles the application's scaling and runtime
configuration. The first deploy takes a minute or two longer while the
registry is provisioned. Builds are [cached by default](#cached-builds-by-default):
a matching image can be reused from the registry without invoking Docker.

## Proxy HTTP to a container port

`getTcpPort(port)` returns a `fetch` handle for any port inside the
container, so an HTTP server you'd normally run in Docker just
works:

```typescript
// inside the DO's inner Effect
const { fetch } = yield* sandbox.getTcpPort(3000);

return {
  hello: () =>
    Effect.gen(function* () {
      yield* sandbox.start({ enableInternet: true });
      const response = yield* fetch(
        HttpClientRequest.get("http://container/"),
      );
      return yield* response.text;
    }),
};
```

The returned `fetch` waits until the port accepts connections, so you
don't coordinate readiness yourself. It never starts the container:
call `start` first.

## Process scope vs request scope

Unlike Workers, a Container is a **real process** — and that changes
what the instance scope means. The bundled program runs under a root
scope that closes when the process shuts down gracefully, so
resources acquired in the constructor (a connection kept warm across requests,
a background consumer) are genuinely released on exit. Serverless
runtimes only approximate this: workerd never closes its instance
scope at all, and Lambda gets a best-effort 500 ms SIGTERM window at
sandbox shutdown. A hard kill still skips finalizers, as in any
process, so treat instance-level cleanup as best-effort.

Each incoming request to the container's HTTP server still gets its
own request `Scope`, released when the response settles — the same
per-event contract as every other runtime. See
[Instance scope vs request scope](/infrastructure-as-effects/runtime#instance-scope-vs-request-scope)
for the model across all runtimes.

## Bring your own image

`main` is one of three image sources. Point `context` at a directory
with a Dockerfile to build your own image, or `image` at a pre-built
remote image that alchemy pulls and re-pushes — no Effect bundling,
no `.make()`, props declared inline on the class:

```typescript
export class Web extends Cloudflare.Container<Web>()("Web", {
  context: `${import.meta.dirname}/context`,
}) {}

export class Echo extends Cloudflare.Container<Echo>()("Echo", {
  image: "mendhak/http-https-echo:latest",
}) {}
```

Arbitrary images expose no RPC methods — the DO talks to them purely
over their TCP port via `getTcpPort`. To build, tag, and push that
image as part of the same Stack, see
[Build & push images](/docker/build-and-push) in the Docker hub.

When `image` already references Cloudflare's managed registry —
for example a digest reference pushed by CI, like
`registry.cloudflare.com/<accountId>/app@sha256:...` — alchemy
deploys the reference as-is and skips the docker pull and push
entirely.

alchemy pulls an `image` as `linux/amd64` on every deploy and every
`alchemy dev` start, so a mutable tag like `:latest` picks up a newly
pushed image. An image that is only in your local Docker, built on
your machine or loaded with `docker load`, has no registry to pull
from, so alchemy uses the local copy:

```typescript
// docker build --platform linux/amd64 -t local-only/app:dev .
export class App extends Cloudflare.Container<App>()("App", {
  image: "local-only/app:dev",
}) {}
```

Build it for `linux/amd64`, the platform Cloudflare runs. When an
image can be neither pulled nor found locally, the error names the
image and the `docker login` or `docker build` command that fixes it.
The tag is the image's identity, so give each build its own tag to
redeploy it.

## Cached builds by default

Dockerfile builds, inline Dockerfiles, and Effect-native `main` builds use
registry caching without extra configuration:

```typescript
export class Web extends Cloudflare.Container<Web>()("Web", {
  context: `${import.meta.dirname}/context`,
}) {}
```

The default repository is
`registry.cloudflare.com/<account-id>/<application-physical-name>`.
The generated physical name includes the stack, stage, and resource-instance
identity. Updates to that application within the stage can reuse published
images, including when reverting to previously built inputs. Replacement or
destroy/recreate can change the physical name and start a new cache.

This is resource-specific caching within a stage, not one shared repository
for every container. To reuse images across stages or applications, choose
a common `publish.repository` as shown below.

:::caution[Pin external build inputs]
Pin base images and downloaded dependencies. A changed upstream image tag or
package download is outside the build context and cannot invalidate its
content hash, so a matching cached image may still be reused.
:::

## Share published images across stages

Set `publish.repository` to choose a destination repository shared by
applications and stages in the same Cloudflare account:

```typescript
export class Web extends Cloudflare.Container<Web>()("Web", {
  context: `${import.meta.dirname}/context`,
  publish: { repository: "web" },
}) {}
```

`"web"` is the repository name, not a source image, application name,
registry host, or complete image reference. Alchemy lowercases it and adds
the configured account ID. With the default registry host, a build uses:

```text
Published build: registry.cloudflare.com/<account-id>/web:<build-hash>
Build cache:     registry.cloudflare.com/<account-id>/web:buildcache
Deployed image:  registry.cloudflare.com/<account-id>/web@sha256:<manifest-digest>
```

The build hash identifies the inputs; the manifest digest identifies the
published artifact. Stages using the same repository and matching build
inputs reuse the published image without invoking Docker. Changed inputs
produce a new hash tag in that repository and can reuse its shared inline
build-layer cache. Each stage still has its own Container application,
runtime settings, and running instances; only images and build layers are shared.

All builds targeting a repository import reusable layers from its
`:buildcache` tag, even when their complete input hashes differ. That tag
points to the latest exported inline cache, not an aggregate of every image
ever published to the repository. A finished-image cache hit leaves the
layer-cache tag unchanged. The Container deploys the immutable digest, not
the mutable `buildcache` tag.

For an Effect-native Container, put `publish` in the props passed to
`.make()`, alongside `main`. Omitting `publish` keeps the default cached,
application-specific repository.

For a pre-built external `image`, `publish.repository` chooses where it is
re-published, but does not enable finished-image cache reuse. Images already
in the target registry keep their existing repository instead of being
copied into the requested one.

## Configure the container with `env`

A container is a process, not a Worker: it has no bindings, so every
piece of configuration reaches it as an environment variable. `env`
takes a plain map and lands each entry on the deployment, where the
program reads it from `process.env` — the same for a generated
(`main`) image, a `context` build, and a pre-built `image`:

```typescript
export class Api extends Cloudflare.Container<Api>()("Api", {
  context: `${import.meta.dirname}/api`,
  ports: [{ name: "http", port: 8080 }],
  env: {
    PORT: "8080",
    LOG_LEVEL: "info",
  },
}) {}
```

Values are strings. Wrap a secret in `Redacted` and it stays
encrypted in alchemy's state and out of plan output, while the
container still reads a plain string:

```typescript
env: {
  SESSION_KEY: Redacted.make(process.env.SESSION_KEY!),
}
```

## Read another resource's outputs

A class body is module scope, so there is nowhere to `yield*` the
bucket, queue, or database whose output you need — and a bare
declaration is an Effect, not a resolved handle, so
`Uploads.bucketName` is `undefined` until something yields it. Pass
the props as an `Effect.gen` instead; inside it, sibling resources
resolve like anywhere else:

```typescript
import * as Effect from "effect/Effect";

export const Uploads = Cloudflare.R2.Bucket("Uploads");

export class Api extends Cloudflare.Container<Api>()(
  "Api",
  Effect.gen(function* () {
    const uploads = yield* Uploads;
    return {
      context: `${import.meta.dirname}/api`,
      env: { BUCKET_NAME: uploads.bucketName },
    };
  }),
) {}
```

The reference is what orders the deploy: the bucket is created
before the container application that reads its name.

## Connect to a SQL database

An **effectful** (`main`) container runs your Effect program, so it
resolves a database capability the same way a Worker does — declare
the connection once, then bind it:

```typescript
// src/Db.ts
import * as Prisma from "@alchemy.run/prisma";
import * as Effect from "effect/Effect";

export const Connection = Effect.gen(function* () {
  const project = yield* Prisma.Project("app", {
    createDatabase: false,
    region: "us-east-1",
  });
  const postgres = yield* Prisma.Postgres("db", {
    project,
    region: "us-east-1",
  });
  return yield* Prisma.Connection("api", { database: postgres });
});
```

```typescript
// src/Api.runtime.ts
import * as Prisma from "@alchemy.run/prisma";
import * as SQL from "@alchemy.run/sql/Postgres";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Api } from "./Api.ts";
import { Connection } from "./Db.ts";

export default Api.make(
  { main: import.meta.url },
  Effect.gen(function* () {
    const db = yield* Prisma.Connect(Connection);
    const sql = yield* SQL.Postgres({ url: db.databaseUrl });

    return Api.of({
      fetch: Effect.gen(function* () {
        const users = yield* sql`SELECT * FROM users`;
        return yield* HttpServerResponse.json(users);
      }),
    });
  }).pipe(Effect.provide(Prisma.ConnectBinding)),
);
```

You never name `DATABASE_URL`. The container has no bindings — it is
a process, not a Worker — so `Prisma.Connect` writes the connection's
outputs onto the deployment as environment variables and reads them
back at runtime. The capability owns both ends.

Start it with outbound networking, or the container never reaches the
database at all:

```typescript
yield* api.start({ enableInternet: true });
```

## Connect an arbitrary image to a database

An image you brought yourself knows nothing about alchemy, so there
is no capability to bind — you name the variable and hand it the
connection string. Give it the provider's **pooled** endpoint
(Prisma's `databaseUrl` already prefers it; on Neon it is
`branch.pooledConnectionUri`, on PlanetScale `role.connectionUrlPooled`),
since nothing else is pooling those connections on the container's
behalf:

```typescript
// src/Web.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import { Connection } from "./Db.ts";

export class Web extends Cloudflare.Container<Web>()(
  "Web",
  Effect.gen(function* () {
    const connection = yield* Connection;
    return {
      context: `${import.meta.dirname}/web`,
      ports: [{ name: "http", port: 8080 }],
      env: {
        DATABASE_URL: connection.databaseUrl,
        PORT: "8080",
      },
    };
  }),
) {}
```

Your image reads `DATABASE_URL` with whatever client it already
uses — `pg`, Prisma, Rails, Django. Nothing about the container is
alchemy-specific; only the value's provenance is.

A Worker fronting the same database still binds Hyperdrive — see
[Hyperdrive](/cloudflare/data/hyperdrive). The two coexist: point
Hyperdrive at the *direct* origin (it pools for the Worker) and the
container at the pooled one.

Under `alchemy dev` the same wiring targets your local emulators. A
local dev database hands out `localhost` URLs, but inside the Docker
container `localhost` is the container itself — so the dev runtime
rewrites loopback hosts in the container's env to
`host.docker.localhost`. The alias deliberately keeps `localhost` in
the hostname so clients that gate plain-HTTP on a local-looking host
(Prisma's `prisma+postgres://` protocol, for one) keep working against
the local dev server.

On Docker Desktop that alias is Docker's `host-gateway`, which
forwards to processes bound to `127.0.0.1` on your machine.

On native Linux, `host-gateway` is the docker bridge IP
(`172.17.0.1`). A TCP SYN to that address is host INPUT — UFW,
firewalld, and nftables on CachyOS/Arch will drop it. Binding the
same port on the bridge or on your LAN NIC does not skip INPUT.
Alchemy does not go that way:

1. `@prisma/dev` (and any host server you start) stay on `127.0.0.1`.
2. A unix socket on the host forwards to that port.
3. The workerd `<name>-proxy` sidecar bind-mounts the socket and maps
   `host.docker.localhost` to `127.0.0.1` in the shared netns.
4. A Docker-managed helper shares that network namespace, accepts
   `127.0.0.1:<port>`, and forwards through the socket. Alchemy waits
   for its listeners before completing container startup and removes
   the helper when the container is deleted or the dev runtime stops.

The container talks to itself. The socket crosses the boundary. UFW
never sees a SYN to `172.17.0.1`.

The helper uses `node:22-alpine`, which Docker downloads on first use.
Normal Docker access is sufficient; Alchemy does not require host-side
`nsenter` permissions. If the helper fails to start, container startup
reports the error.

Neon and PlanetScale have no local emulator: even under `alchemy
dev` the URL is a cloud host (`*.neon.tech`, `*.psdb.cloud`) and the
rewrite is a no-op. The container still needs
`enableInternet: true` to reach them.

## Which capabilities reach a container

The split follows the transport. A capability whose config travels as
environment variables works in a container: `Prisma.Connect`, and the
token-scoped HTTP clients for
[R2](/cloudflare/data/r2), [KV](/cloudflare/data/kv), and
[Queues](/cloudflare/messaging/queues) —
`Cloudflare.R2.ReadWriteBucketHttp` and friends, which mint a scoped
API token instead of using a native binding.

A capability that *is* a workerd binding cannot:
`Cloudflare.Hyperdrive.Connect`, `Cloudflare.D1.QueryDatabase`, and
every `*Binding` layer resolve against the Workers runtime, which no
container process has. Use the `*Http` layer where one exists, and
environment variables otherwise.

## Bind on an async Worker

An async Worker can host a container-backed Durable Object class
that ships as plain JavaScript — `@cloudflare/sandbox`'s `Sandbox`,
or your own class extending `@cloudflare/containers`' `Container`.
The class lives in the worker script:

```typescript
// src/worker.ts
import { Container } from "@cloudflare/containers";

export class Sandbox extends Container {
  defaultPort = 8080;
}
```

`Sandbox` is the Durable Object class Cloudflare instantiates for
each container instance; `Container` (from `@cloudflare/containers`)
handles the lifecycle and forwards `fetch` to the port inside it.

Declare it in the stack by binding a `Cloudflare.Container` in the
Worker's `env` — the Container is the Durable Object binding and its
ContainerApplication together. Alchemy emits the
`durable_object_namespace` binding, marks the class as
container-backed in the script metadata, provisions the application,
and attaches it to the class's namespace:

```typescript
// alchemy.run.ts
import type { Sandbox } from "./src/worker.ts";

export const Worker = Cloudflare.Worker("Worker", {
  main: "./src/worker.ts",
  env: {
    Sandbox: Cloudflare.Container<Sandbox>("Sandbox", {
      image: "docker.io/cloudflare/sandbox:0.1.3",
    }),
  },
});
```

The Durable Object class name defaults to the binding name (the
`env` key). Set `className` when the exported class is named
differently. The type parameter (`Container<Sandbox>`) is the class
from `worker.ts` above — it types `env.Sandbox` as
`DurableObjectNamespace<Sandbox>` through
[`InferEnv`](/cloudflare/compute/workers#async-workers),
so the handler reaches the container with full types:

```typescript
// src/worker.ts
import { getContainer } from "@cloudflare/containers";
import type * as Cloudflare from "@alchemy.run/cloudflare";
import type { Worker } from "../alchemy.run.ts";

export default {
  async fetch(request: Request, env: Cloudflare.InferEnv<typeof Worker>) {
    return getContainer(env.Sandbox, "default").fetch(request);
  },
};
```

Only image-backed containers (`image`, or `context`/`dockerfile`)
can be bound this way. An effectful (`main`) container's runtime is
provided by its `.make()` Layer, which needs an Effect-native
Durable Object host.

## Several images in one Durable Object

A Durable Object can choose its image at runtime. Declare one Container
per image with `schedulingPolicy: "durable_object"`:

```typescript
export class Node extends Cloudflare.Container<Node>()("Node", {
  schedulingPolicy: "durable_object",
  image: "node:24-slim",
}) {}

export class Python extends Cloudflare.Container<Python>()("Python", {
  schedulingPolicy: "durable_object",
  dockerfile: "./python/Dockerfile",
  context: "./python",
}) {}
```

One Container is one image: `image` copies a registry image, and
`context`/`dockerfile` builds one. Alchemy publishes it to Cloudflare's
registry and prepares it before the Worker is uploaded.

Bind both in the Durable Object. Each handle's `start()` starts its own
image:

```typescript
export class Agent extends Cloudflare.DurableObject<Agent>()(
  "Agent",
  Effect.gen(function* () {
    const node = yield* Node;
    const python = yield* Python;
    return Effect.succeed({
      run: (cmd: string[]) =>
        Effect.gen(function* () {
          yield* node.start({
            instance: "lite",
            entrypoint: ["sleep", "infinity"],
            enableInternet: false,
          });
          return yield* (yield* node.exec(cmd)).output();
        }).pipe(Effect.scoped),
    });
  }),
) {}
```

Cloudflare gives a Durable Object class one container application, and
each object one container. Every binding adds its image to the class's
application under the Container's id. The handles control the same
container, so start one image at a time and `destroy()` before you
switch.

Application-wide settings — `ssh`, `authorizedKeys`, and
`observability` — can be declared on any of the class's Containers.
Two Containers that declare different values fail the deploy with
`ContainerApplicationSettingsConflict`.

A Container can be bound by several Durable Object classes. Each class adds
the image to its own application.

## Several images on an async Worker

A plain Durable Object class lists its Containers on its binding. A
Container can back several classes:

```typescript
// alchemy.run.ts
const Node = Cloudflare.Container("Node", {
  schedulingPolicy: "durable_object",
  image: "node:24-slim",
});

const Python = Cloudflare.Container("Python", {
  schedulingPolicy: "durable_object",
  image: "python:3.13-slim",
});

const Rust = Cloudflare.Container("Rust", {
  schedulingPolicy: "durable_object",
  image: "rust:1.90-slim",
});

export const Worker = Cloudflare.Worker("App", {
  main: "./src/worker.ts",
  env: {
    AGENT: Cloudflare.DurableObject("Agent", { containers: [Node, Python] }),
    REVIEWER: Cloudflare.DurableObject("Reviewer", { containers: [Node, Rust] }),
  },
});
```

Each class gets its own container application. The class finds each
image in `ctx.container.images` under its Container's id:

```typescript
// src/worker.ts
import { DurableObject } from "cloudflare:workers";

export class Agent extends DurableObject {
  async run(cmd: string[]) {
    const container = this.ctx.container!;
    if (!container.running) {
      container.start({
        image: container.images.Node,
        entrypoint: ["sleep", "infinity"],
        enableInternet: false,
      });
    }
    return (await container.exec(cmd)).output();
  }
}
```

Only fleet Containers bind directly in `env`. A Durable Object-managed
Container placed there fails with `DurableObjectContainerBindingError`.

## ContainerApplication

Under the hood every container is backed by a
[ContainerApplication](/providers/cloudflare/containers/containerapplication)
— the deployed, scalable unit that carries the image, instance
type, instance counts, and observability settings. You typically
extend `Cloudflare.Container` rather than using it directly, but
the same props shape (`instanceType`, `observability`, `runtime`,
…) flows through `.make()`.

## Rollouts

When a deploy changes the image or configuration, running instances
are replaced with the new version. By default the replacement is
immediate — every instance at once. Set `rollout` to replace them in
steps instead, so the application stays available through the update:

```typescript
export class Api extends Cloudflare.Container<Api>()("Api", {
  main: import.meta.url,
  instances: 4,
  maxInstances: 4,
  rollout: { strategy: "rolling", stepPercentage: 25 },
}) {}
```

A `rolling` strategy replaces `stepPercentage` of the instances per
step and advances automatically as the new instances come up. Each
replaced instance receives `SIGTERM` and has 15 minutes to shut down
cleanly before `SIGKILL`.

Rollouts replace instances; they do not split requests between two
image versions. The Worker and Durable Object fronting the container
cut over immediately while instances roll, so keep the
Worker-to-container protocol compatible across both image versions
until the rollout completes. Request-level traffic splitting exists
one layer up, on the Worker — see
[Gradual deployments](/cloudflare/compute/gradual-deployments).

## Where next

The full walkthrough:

- [Run a Container](/cloudflare/compute/run-a-container) — build the
  sandbox above out to a shell-executing container with RPC, HTTP
  proxying, stage-dependent config, and tests.

Related:

- [Container lifecycle](/cloudflare/compute/container-lifecycle) — start,
  stop, sleep, and snapshot a container.
- [Durable Objects](/cloudflare/compute/durable-objects) — every container
  is fronted by one.
- [Workers](/cloudflare/compute/workers) — the entrypoint that reaches the
  DO (and through it, the container).

Reference:

- [Container API reference](/providers/cloudflare/containers/container)
- [ContainerApplication API reference](/providers/cloudflare/containers/containerapplication)