An Aspire 13 event-ticketing AppHost must ship to Docker Compose on an on-premises host and to Azure Container Apps in production; how do you model that?
answer
- one compute environment per target
- WithComputeEnvironment or no default
- branch on builder.Environment
- Compose output includes a dashboard
- ACA: registry, identity, HTTPS upgrade
basics
~10 sAdd AddDockerComposeEnvironment and AddAzureContainerAppEnvironment to the AppHost and either branch on builder.Environment so each deploy sees one target, or bind every compute resource with WithComputeEnvironment. Then aspire deploy --environment picks the run.
solid answer
~40 sEach target is a compute environment resource: `AddDockerComposeEnvironment("onprem")` from `Aspire.Hosting.Docker` and `AddAzureContainerAppEnvironment("aca")` from `Aspire.Hosting.Azure.AppContainers`. With both in the model there is no default, so either bind each project with `WithComputeEnvironment(...)` or — simpler for one app going to two places — add only one environment depending on `builder.Environment`, since `aspire deploy --environment onprem` runs the AppHost in that environment. The Compose target writes `docker-compose.yaml` plus `.env.onprem`, runs `docker compose up -d --remove-orphans`, and includes an Aspire dashboard container unless you call `WithDashboard(false)`. The Container Apps target provisions the environment, an Azure Container Registry and managed identity, pushes images and creates the apps; it upgrades external HTTP endpoints to HTTPS. Teams moving from `azd` add `WithAzdResourceNaming()` to keep existing resource names.
code
csharp · 20 linesvar builder = DistributedApplication.CreateBuilder(args);
var onprem = builder.AddDockerComposeEnvironment("onprem").WithDashboard(false);
var aca = builder.AddAzureContainerAppEnvironment("aca").WithAzdResourceNaming();
// Public site in Azure
var tickets = builder.AddPostgres("postgres").WithComputeEnvironment(aca)
.AddDatabase("ticketsdb");
builder.AddProject<Projects.Ticketing_Api>("ticketing-api")
.WithReference(tickets)
.WithExternalHttpEndpoints()
.WithComputeEnvironment(aca);
// Gate scanning at the venue, on the Docker host
var gateCache = builder.AddRedis("gate-cache").WithComputeEnvironment(onprem);
builder.AddProject<Projects.Gate_Scanner>("gate-scanner")
.WithReference(gateCache)
.WithComputeEnvironment(onprem);
builder.Build().Run();go deeper
Recall that each deployment target is an environment resource added to the AppHost, such as AddDockerComposeEnvironment or AddAzureContainerAppEnvironment.
Explain both modelling options: branching on builder.Environment so each run has one target, or binding resources with WithComputeEnvironment.
Anticipate target differences: the Compose dashboard container, ACA's registry, identity and HTTPS upgrade, managed backing services in Azure, and azd naming when migrating.
Weigh one AppHost serving several targets against separate deployment definitions, considering drift between environments and who owns each target's operations.
## The shape of the problem The ticketing solution has a public `ticketing-api`, an internal `checkout-worker`, PostgreSQL and Redis. Two deployments are needed: a single on-premises Docker host for a venue's box office, and Azure Container Apps for the public site. In Aspire 13 a deployment target is a **compute environment** resource in the AppHost, so the question is how to put two of them in one model. ## Option 1: one environment per run Because `aspire deploy --environment <name>` runs the AppHost with that environment name, the AppHost can add exactly the target that run needs: ```csharp var builder = DistributedApplication.CreateBuilder(args); if (builder.Environment.IsEnvironment("onprem")) builder.AddDockerComposeEnvironment("onprem"); else if (builder.Environment.IsProduction()) builder.AddAzureContainerAppEnvironment("aca"); var tickets = builder.AddPostgres("postgres").AddDatabase("ticketsdb"); builder.AddProject<Projects.Ticketing_Api>("ticketing-api") .WithReference(tickets) .WithExternalHttpEndpoints(); ``` Each deploy sees a single compute environment, so every compute resource attaches to it automatically; a local `aspire run` in Development adds neither. Because `--environment` defaults to `Production`, a plain `aspire deploy` targets Azure. Deployment state is cached per environment, and Compose writes a separate `.env.onprem`. ## Option 2: both environments, explicit binding When one model should place **different** resources on different targets — say a gate-scanner service and its Redis cache run at the venue while the public API runs in Azure — add both environments and bind each compute resource (projects and containers such as the PostgreSQL server; a database resource follows its server): - `.WithComputeEnvironment(onprem)` on resources for the Docker host; - `.WithComputeEnvironment(aca)` on resources for Azure. Forgetting one fails the pipeline with an `InvalidOperationException` that names the unassigned resources and lists the environments, rather than silently skipping them. ## What the Compose target does 1. `aspire publish` writes `docker-compose.yaml` and an unfilled `.env` to `aspire-output`. 2. `aspire deploy --environment onprem` resolves parameters into `.env.onprem`, builds images and runs `docker compose up -d --remove-orphans`. 3. The generated Compose file includes an **Aspire dashboard** container by default, pinned to the Aspire major.minor tag (for example `13.6`) and wired to receive OTLP; `WithDashboard(false)` leaves it out. 4. `PublishAsDockerComposeService(...)` customises one service; `ConfigureComposeFile(...)` and `ConfigureEnvFile(...)` edit the whole file. 5. Docker or Podman 5+ is detected automatically; `ASPIRE_CONTAINER_RUNTIME` forces one. ## What the Container Apps target does - Provisions the Container Apps environment, an **Azure Container Registry**, a **managed identity** with pull rights, and one container app per compute resource, then pushes images. - Uses Azure CLI credentials by default, so `az login` comes first. - Maps endpoints to ingress: an external HTTP endpoint becomes public ingress over the platform's HTTPS endpoint; internal ones stay inside the environment. - Upgrades external HTTP endpoints to HTTPS in generated URLs and service-discovery values; `WithHttpsUpgrade(false)` keeps `http://`. - `WithDashboard(false)` drops the dashboard it adds; `PublishAsAzureContainerApp(...)` customises one app's generated resource. - `WithAzdResourceNaming()` keeps the naming scheme an earlier `azd` deployment used, avoiding duplicate resources. ## Backing services differ per target On the Docker host, PostgreSQL and Redis run as Compose services from the same container images Aspire uses locally. In Azure, the docs recommend managed services as the production default for databases and caches, which usually means a different hosting resource in the `aca` branch — for example `AddAzurePostgresFlexibleServer` — while the API still receives a `ConnectionStrings__ticketsdb` value. ## Comparing the two targets | | Docker Compose environment | Azure Container Apps environment | |---|---|---| | Package | `Aspire.Hosting.Docker` | `Aspire.Hosting.Azure.AppContainers` | | Publish output | `docker-compose.yaml`, `.env` | Bicep | | Deploy action | `docker compose up -d --remove-orphans` | provision and update Azure resources | | Images | built locally for the host | built and pushed to the provisioned registry | | Dashboard | container in the Compose file | added to the environment | | Credentials | the Docker host's | Azure CLI login by default |
- Why does a generated Compose file contain an aspire-dashboard service you never declared?The Docker Compose environment includes a dashboard by default (`DashboardEnabled` is true) and wires services to send it OTLP. Its image tag is pinned to the Aspire major.minor that generated the file, so it does not drift to `latest`. Call `WithDashboard(false)` on the environment to omit it, for example when the host already ships telemetry elsewhere.
- What does WithAzdResourceNaming solve when moving from azd to aspire deploy?`aspire deploy` and `azd` name Azure resources differently by default. On an environment first deployed with `azd`, switching to `aspire deploy` without `WithAzdResourceNaming()` would create new, duplicate resources instead of updating the existing ones.
- With two compute environments, Aspire deploys every resource to both automatically.
- aspire deploy --environment only renames the output folder; the AppHost code is unaffected.
- The Container Apps target reuses the Compose file to create the container apps.
- Compose output never includes a dashboard, so telemetry has nowhere to go.
- Switching an azd-deployed environment to aspire deploy keeps resource names unchanged by default.
Assumes Aspire 13.6: compute environments with WithComputeEnvironment binding, the multiple-environment validation error, and the major.minor-pinned dashboard image in Compose output are current 13.x behaviour.