Cloudflare Containers
Deploy a .NET project as a Cloudflare Container — fronted by a Worker and a Durable Object, with the image built and pushed by wrangler.
Deploy a project as a container
Section titled “Deploy a project as a container”builder.AddCloudflareEnvironment();
builder.AddProject<Projects.Api>("api") .PublishAsCloudflareContainer();await builder.addCloudflareEnvironment();await builder.addProject('api', '../Api/Api.csproj').publishAsCloudflareContainer();aspire run→ the project runs locally as a normal Aspire project.aspire deploy→ the project is published, a Dockerfile + Worker +wrangler.jsoncare generated, andwrangler deploybuilds the image (linux/amd64), pushes it to Cloudflare’s registry, and deploys the Worker.
What gets generated
Section titled “What gets generated”| File | Purpose |
|---|---|
Dockerfile |
Runs the published app on mcr.microsoft.com/dotnet/aspnet:10.0 (linux/amd64), listening on the configured port. |
worker.js |
A Durable Object class extending the @cloudflare/containers Container, plus a fetch handler that forwards requests to a container instance. |
wrangler.jsonc |
Wires the containers, durable_objects binding, and migrations. |
Options
Section titled “Options”.PublishAsCloudflareContainer(options =>{ options.WorkerName = "orders-api"; // defaults to a sanitized resource name options.Port = 8080; // the app is configured to listen here options.MaxInstances = 5; options.InstanceType = "standard"; // dev | basic | standard (optional) options.SleepAfter = "10m"; // idle timeout before an instance sleeps});await api.publishAsCloudflareContainer({ configure: async options => { await options.workerName.set('orders-api'); await options.port.set(8080); await options.maxInstances.set(5); await options.instanceType.set('standard'); await options.sleepAfter.set('10m'); }});Requirements
Section titled “Requirements”- Docker running locally (wrangler builds the image).
- wrangler installed — see How wrangler authenticates.
- API token with Workers Scripts: Edit — see API Tokens.
Environment & configuration
Section titled “Environment & configuration”The project’s resolved environment variables — connection strings from WithReference, anything from WithEnvironment — are injected into the container via the Worker’s envVars, so your app reads its configuration exactly as it does locally. Local-only variables (OpenTelemetry endpoints, service-discovery URLs, the local listen URL) are filtered out.
builder.AddProject<Projects.Api>("api") .WithReference(uploads) // an R2 bucket — its connection string is injected .WithEnvironment("FEATURE_FLAG", "on") .PublishAsCloudflareContainer();await builder.addProject('api', '../Api/Api.csproj') .withReference(uploads) .withEnvironment('FEATURE_FLAG', 'on') .publishAsCloudflareContainer();At deploy, the R2 connection string resolves to the real R2 endpoint and credentials, so the container reaches R2 in production with no code change.
Custom domains
Section titled “Custom domains”var zone = builder.AddDeploymentParameter("zone-id");var host = builder.AddDeploymentParameter("api-hostname");
builder.AddProject<Projects.Api>("api") .PublishAsCloudflareContainer() .WithCustomDomain(host, zone);const zone = await builder.addDeploymentParameter('zone-id');const host = await builder.addDeploymentParameter('api-hostname');await builder.addProject('api', '../Api/Api.csproj') .publishAsCloudflareContainer() .withCustomDomainParameters(host, zone);See Custom Domains.
