OpenAICloudflare Developer PlatformMar 26, 2026, 12:00 AM

Containers - Easily connect Containers and Sandboxes to Workers

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

Containers - Easily connect Containers and Sandboxes to Workers

Key Points

  • Outbound Workers over HTTP from containers
  • Access KV, R2, and Durable Objects via hostnames
  • Upgrade to containers 0.2.0 or sandbox 0.8.0

Summary

Containers and Sandboxes can now route HTTP outbound requests to Cloudflare Workers. From inside a container or sandbox, requests to configured hostnames are intercepted and handled by Worker outbound handlers, letting your container code call Worker functions and use Worker bindings (KV, R2, Durable Objects) directly.

Key Points

  • How it works

    • Containers/Sandboxes intercept HTTP requests and dispatch them to Worker handlers defined via outbound or outboundByHost.
    • Handlers execute inside the Workers runtime (outside the container sandbox) and have access to env and ctx.
  • Bindings and storage

    • Use hostnames to map to bindings: e.g. curl http://my.kv/some-key -> Worker can call env.KV.get(...); curl http://my.r2/some-object -> Worker can access R2.
    • Durable Object access: use ctx.containerId and env.MY_CONTAINER.idFromString(ctx.containerId) to get a stub scoped to the container instance.
  • Security and TLS

    • Currently only HTTP interception is supported; HTTPS interception is coming soon (will enable transparent credential injection).
    • Traffic to Workers is routed on the same machine as the container; you can upgrade requests to TLS from the Worker if needed.
  • Upgrade and get started

    • Upgrade to @cloudflare/containers >= 0.2.0 or @cloudflare/sandbox >= 0.8.0 to enable outbound Workers.
    • Define outbound to capture all outbound HTTP requests or outboundByHost to map specific hostnames to handlers.

Practical tips

  • Prefer outboundByHost for binding-style hostnames (e.g. my.kv, my.r2, get-state.do).
  • Keep handlers lightweight; they run in the Workers runtime and should return a Response for the container's HTTP client.
  • For state that should be tied to a container instance, use the provided Durable Object id via ctx.containerId.

Key docs

Refer to the "Containers outbound traffic" and "Sandboxes outbound traffic" docs in the Cloudflare docs for examples and full API details.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

Containers — コンテナとサンドボックスを Workers に簡単に接続

Containers — コンテナとサンドボックスを Workers に簡単に接続

公開日: 2026-03-26

概要

Containers と Sandboxes で、HTTP 経由で Workers に直接接続できるようになりました。これにより、コンテナ内部から特定のホスト名を通じて Workers の関数やバインディング(例: KV、R2)を呼び出せます。

Worker コードの実行

任意の HTTP リクエストを捕捉する outbound ハンドラーを定義するか、特定のホスト名や IP に対するリクエストを捕捉する outboundByHost を使います。以下は例です。

export class MyApp extends Sandbox {}
MyApp.outbound = async (request, env, ctx) => {
    // 任意の Worker 内で定義された関数を任意の HTTP リクエストで実行できます
    return await someWorkersFunction(request.body);
};
MyApp.outboundByHost = {
    "my.worker": async (request, env, ctx) => {
        return await anotherFunction(request.body);
    },
};

この例では、コンテナから http://my.worker へのリクエストは outboundByHost 内で定義した関数が実行され、それ以外の HTTP リクエストは outbound ハンドラーが実行されます。これらのハンドラーはコンテナのサンドボックス外、Workers ランタイム内で完全に実行されます。

TLS サポート(近日対応)

現時点では Containers と Sandboxes は HTTP トラフィックのみをインターセプトします。HTTPS のインターセプトは近日対応予定です。これにより、資格情報注入のために Workers を透過プロキシとして利用できるようになります。現在でも Workers へのトラフィックは安全で、コンテナと同じマシン上で実行されます。必要に応じて、Worker 側でリクエストを TLS にアップグレードすることも可能です。

Workers バインディングへのアクセス

各ハンドラーは env にアクセスできるため、Wrangler の設定で定義したあらゆるバインディングを呼び出せます。コンテナ内のコードはそのホスト名に対して通常の HTTP リクエストを行い、outbound Worker がそれをバインディング呼び出しに変換します。

export class MyApp extends Sandbox {}
MyApp.outboundByHost = {
    "my.kv": async (request, env, ctx) => {
        const key = new URL(request.url).pathname.slice(1);
        const value = await env.KV.get(key);
        return new Response(value ?? "", { status: value ? 200 : 404 });
    },
    "my.r2": async (request, env, ctx) => {
        const key = new URL(request.url).pathname.slice(1);
        const object = await env.BUCKET.get(key);
        return new Response(object?.body ?? "", { status: object ? 200 : 404 });
    },
};

この状態でコンテナ内から curl http://my.kv/some-key を実行すると Workers KV にアクセスでき、curl http://my.r2/some-object を実行すると R2 にアクセスできます。

Durable Object の状態にアクセス

ctx.containerId を使ってコンテナに自動プロビジョニングされる Durable Object を参照できます。例:

export class MyContainer extends Container {}
MyContainer.outboundByHost = {
    "get-state.do": async (request, env, ctx) => {
        const id = env.MY_CONTAINER.idFromString(ctx.containerId);
        const stub = env.MY_CONTAINER.get(id);
        return stub.getStateForKey(request.body);
    },
};

これにより、任意のコンテナインスタンスに状態を簡単に紐づけられます。Durable Object には組み込みの SQLite データベースも含まれます。

今すぐ始める

outbound Workers を利用するには、@cloudflare/containers をバージョン 0.2.0 以降、または @cloudflare/sandbox をバージョン 0.8.0 以降にアップグレードしてください。詳細と追加の例は Containers outbound traffic および Sandboxes outbound traffic を参照してください。


© 2026 Cloudflare, Inc. | Privacy Policy | Terms of Use

Containers - Easily connect Containers and Sandboxes to Workers | Cloudflare Developer Platform | DocsDigest