ClaudeCloudflare 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.

claudeenmodel: claude-sonnet-4-20250514

Cloudflare Containers and Sandboxes Now Support Direct HTTP Connection to Workers

Key Points

  • Direct HTTP connection between Containers/Sandboxes and Workers
  • Access Workers bindings (KV, R2) via standard HTTP requests
  • Built-in Durable Object state management with SQLite

Summary

Cloudflare Containers and Sandboxes now support direct HTTP connections to Workers, enabling seamless integration between containerized applications and Workers functions. This feature allows containers to access Workers bindings like KV and R2 storage through standard HTTP requests.

Key Points

  • Outbound HTTP Handlers: Define outbound and outboundByHost handlers to capture and process HTTP requests from containers
  • Workers Bindings Access: Containers can access KV, R2, and other Workers bindings through HTTP calls to specific hostnames
  • Durable Object Integration: Use ctx.containerId to reference automatically provisioned Durable Objects with built-in SQLite database
  • Secure Communication: Traffic between containers and Workers runs securely on the same machine
  • HTTP Only: Currently supports HTTP traffic only; HTTPS interception coming soon
  • Easy Upgrade: Available in @cloudflare/containers v0.2.0+ and @cloudflare/sandbox v0.8.0+

Full Translation

Translations

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

claudejamodel: claude-sonnet-4-20250514

Containers - ContainerとSandboxをWorkersに簡単に接続

ContainerとSandboxをWorkersに簡単に接続

2026年3月26日

ContainerとSandboxがHTTP経由でWorkersに直接接続することをサポートしました。これにより、特定のホスト名でコンテナ内からWorkers関数やKVやR2などのbindingsを呼び出すことができます。

Workerコードの実行

任意のHTTPリクエストをキャプチャするoutboundハンドラーを定義するか、個別のホスト名とIPへのリクエストをキャプチャするoutboundByHostを使用します。

export class MyApp extends Sandbox {}

MyApp.outbound = async (request, env, ctx) => {
  // 任意のHTTPリクエストでWorkerで定義された任意の関数を実行できます
  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サポートは近日公開予定

ContainerとSandboxは現在HTTPトラフィックのみをインターセプトします。HTTPSインターセプションは近日公開予定です。これにより、認証情報の注入のためにWorkersを透過プロキシとして使用できるようになります。

これはHTTPを使用しているだけですが、Workersへのトラフィックは安全で、コンテナと同じマシン上で実行されます。必要に応じて、Worker自体からリクエストをTLSにアップグレードすることもできます。

Workers bindingsへのアクセス

各ハンドラーはenvにアクセスできるため、Wrangler configで設定された任意のbindingを呼び出すことができます。コンテナ内のコードはそのホスト名に標準的なHTTPリクエストを行い、outbound Workerがそれをbinding呼び出しに変換します。

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);
  },
};

これにより、任意のコンテナインスタンスと状態を関連付ける簡単な方法が提供され、組み込みのSQLiteデータベースが含まれます。

今すぐ始める

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

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